<?php
    /**
     * Includes: CPT
     */
    if (! defined('ABSPATH')) {
        die('Sorry, you cannot access this page.');
    }

    /**
     * Add nonces
     */
    add_action('edit_form_after_title', function ($post) {
        if (! $post) {
            return;
        }

        switch ($post->post_type) {
            case 'chama_tier':
                wp_nonce_field('tier_metadata', 'chama_tier_nonce');
                break;
            case 'chama_campaign':
                wp_nonce_field('campaign_metadata', 'chama_campaign_nonce');
                break;
            case 'chama_commission':
                wp_nonce_field('commission_metadata', 'chama_commission_nonce');
                break;
        }
    });

    /**
     * Add featured image description
     */
    function chama_filter_featured_image_admin_text($content, $post_id, $thumbnail_id)
    {
        $screen = get_current_screen();
        if ($screen->post_type === 'chama_tier') {
            $help_text = '<p>' . __('Please use an image that is 600 pixels wide x 300 pixels tall.', 'chama') . '</p>';
            return $help_text . $content;
        }
        if ($screen->post_type === 'chama_campaign' || $screen->post_type === 'chama_commission') {
            $help_text = '<p>' . __('Please use an image that is 854 pixels wide x 480 pixels tall.', 'chama') . '</p>';
            return $help_text . $content;
        }
        return $content;
    }
    add_filter('admin_post_thumbnail_html', 'chama_filter_featured_image_admin_text', 10, 3);
    /*=============================================TIER=============================================*/
    /**
     * Add Tier metaboxes
     */

    //member limit
    add_action('save_post_chama_tier', 'chama_tier_save_postdata', 20, 2);
    add_action('admin_init', 'chama_member_limit_init_metabox');
    if (! function_exists('chama_member_limit_init_metabox')):
        function chama_member_limit_init_metabox()
    {
            add_meta_box(
                'member_limit',
                __('Limit Number of People Who Can Join', 'chama'),
                'chama_member_limit_display_metabox',
                'chama_tier',
                "side",
                "low"
            );

        }
    endif;
    //Displaying the meta box
    if (! function_exists('chama_member_limit_display_metabox')):
        function chama_member_limit_display_metabox($post)
    {
            chama_number_display_metabox($post->ID, 'member_limit', 'This will count down as people join but will not automatically go back up if someone cancels their membership. Set to \'0\' (zero) for sold out. Set to \'-1\' (minus one) for no limit.');
        }
    endif;
    //tier_price
    add_action('admin_init', 'chama_tier_price_init_metabox');
    if (! function_exists('chama_tier_price_init_metabox')):
        function chama_tier_price_init_metabox()
    {
            add_meta_box(
                'tier_price',
                __('Price per Month', 'chama'),
                'chama_tier_price_display_metabox',
                'chama_tier',
                "side",
                "low"
            );

        }
    endif;
    //Displaying the meta box
    if (! function_exists('chama_tier_price_display_metabox')):
        function chama_tier_price_display_metabox($post)
    {
            chama_price_display_metabox($post->ID, 'tier_price', 'Set your price per month. Supporters can choose to pay more.', false, 1, 'For FREE membership level, simply instruct your users to register an account on your website for free and ask them to make sure they verify their email address by following the instructions in the confirmation email.');
        }
    endif;
    //Enable 'Pay What You Want',
    add_action('admin_init', 'chama_tier_is_highlighted_init_metabox');
    if (! function_exists('chama_tier_is_highlighted_init_metabox')):
        function chama_tier_is_highlighted_init_metabox()
    {
            add_meta_box(
                'tier_is_highlighted',
                __('Highlighted Tier', 'chama'),
                'chama_tier_is_highlighted_display_metabox',
                'chama_tier',
                "side",
                "low"
            );

        }
    endif;
    //Displaying the meta box
    if (! function_exists('chama_tier_is_highlighted_display_metabox')):
        function chama_tier_is_highlighted_display_metabox($post)
    {
            chama_checkbox_display_metabox($post->ID, 'tier_is_highlighted', 'If you enable this option, the tier will be highlighted as a promoted tier.', 'Highlight this tier');
        }
    endif;
    //Tier stripe metadata
    add_action('admin_init', 'chama_campaign_analytics_init_metabox');
    if (! function_exists('chama_campaign_analytics_init_metabox')):
        function chama_campaign_analytics_init_metabox()
    {
            add_meta_box(
                'campaign_analytics',
                __('Stripe Metadata', 'chama'),
                'chama_tier_stripe_metadata_display_metabox',
                'chama_tier',
                "side",
                "low"
            );

        }
    endif;
    //Displaying the meta box
    if (! function_exists('chama_tier_stripe_metadata_display_metabox')):
        function chama_tier_stripe_metadata_display_metabox($post)
    {
            $product_id = get_post_meta($post->ID, 'tier_stripe_product_id', true) ? get_post_meta($post->ID, 'tier_stripe_product_id', true) : 0;
            $price_id   = get_post_meta($post->ID, 'tier_stripe_price_id', true) ? get_post_meta($post->ID, 'tier_stripe_price_id', true) : 0;
            if ($product_id) {
                echo '<strong>Product ID:</strong><br/>' . esc_html($product_id) . '<br/><br/>';
            }
            if ($price_id) {
                echo '<strong>Price ID:</strong><br/>' . esc_html($price_id) . '<br/>';
            }

        }
    endif;
    //This function saves the data you put in the meta box
    if (! function_exists('chama_tier_save_postdata')):
        function chama_tier_save_postdata($post_id, $post)
    {
            if ($post->post_type == 'chama_tier') {

                //Not save if the user hasn't submitted changes
                if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
                    return;
                }

                // Verifying whether input is coming from the proper form
                if (
                ! isset($_POST['chama_tier_nonce']) ||
                ! wp_verify_nonce(sanitize_key(wp_unslash($_POST['chama_tier_nonce'])), 'tier_metadata')
            ) {
                    return;
                }

                // Making sure the user has permission
                if (! current_user_can('edit_post', $post_id)) {
                    return;
                }

                if (! empty($_POST['member_limit'])) {
                    /* Get the posted data and sanitize it for use as an HTML class. */
                    $data = intval($_POST['member_limit']);
                    update_post_meta($post_id, 'member_limit', $data);

                } else {
                    update_post_meta($post_id, 'member_limit', 0);
                }

                if (! empty($_POST['tier_price'])) {
                    /* Get the posted data and sanitize it for use as an HTML class. */
                    $data = str_replace(',', '', number_format(floatval($_POST['tier_price']), 2));
                    update_post_meta($post_id, 'tier_price', $data);

                } else {
                    update_post_meta($post_id, 'tier_price', 0);
                }
                if (! empty($_POST['tier_is_highlighted'])) {
                    /* Get the posted data and sanitize it for use as an HTML class. */
                    $data = sanitize_text_field(wp_unslash($_POST['tier_is_highlighted']));
                    if ($_POST["tier_is_highlighted"] == "yes") {
                        $tier_is_highlighted = "yes";
                    } else {
                        $tier_is_highlighted = "no";
                    }
                    update_post_meta($post_id, 'tier_is_highlighted', $tier_is_highlighted);
                } else {
                    delete_post_meta($post_id, 'tier_is_highlighted');
                }
                //save to stripe
                if (isset($_POST['post_title']) && ! empty($_POST['post_title'])) {
                    // The post title is set
                    try {
                        $secret_key = chama_is_live_mode() ? CHAMA_STRIPE_LIVE_SECRET_KEY : CHAMA_STRIPE_TEST_SECRET_KEY;

                        if ($secret_key) {
                            $stripe = new \ChamaWP\Vendor\Stripe\StripeClient(
                                $secret_key // API Secret here
                            );
                            $product_id = '';
                            //add the product to stripe
                            if (get_post_meta($post_id, 'tier_stripe_product_id', true)) {
                                $product_id = get_post_meta($post_id, 'tier_stripe_product_id', true);
                            } else {
                                $product_args = [
                                    'name' => sanitize_text_field(wp_unslash($_POST['post_title'])),
                                ];
                                $product_response = $stripe->products->create($product_args);
                                if ($product_response->id) {
                                    $product_id = $product_response->id;
                                    update_post_meta($post_id, 'tier_stripe_product_id', $product_id);

                                    $stripe->products->update(
                                        $product_id,
                                        ['metadata' => ['tier_id' => $post_id]]
                                    );

                                }
                            }
                            if ($product_id) {
                                $tier_price_cents = sanitize_text_field(wp_unslash($_POST['tier_price'])) * CHAMA_CURRENCY_MINIMUM;
                                /*
                            $recurring_interval_args = [
                            'interval' => 'month',
                            ];
                             */
                                $recurring_interval_args = [
                                    'interval' => CHAMA_SUBSCRIPTION_INTERVAL,
                                ];
                                if (get_post_meta($post_id, 'tier_stripe_price_id', true)) {
                                    $price_id = get_post_meta($post_id, 'tier_stripe_price_id', true);
                                    $price    = $stripe->prices->retrieve($price_id, []);

                                    if ($price->unit_amount) {

                                        if ((int) $price->unit_amount !== (int) $tier_price_cents) {

                                            //delete previous price
                                            $stripe->products->update(
                                                $product_id,
                                                ['default_price' => '']
                                            );
                                            $deletion_args = [
                                                'active' => false,
                                            ];

                                            $stripe->prices->update(
                                                $price_id,
                                                $deletion_args
                                            );
                                            //add new price
                                            chama_add_price($post_id, $product_id, $tier_price_cents, $recurring_interval_args);
                                        }
                                    }
                                    $stripe->prices->update($price_id, ['unit_amount' => $tier_price_cents]);
                                } else {

                                    chama_add_price($post_id, $product_id, $tier_price_cents, $recurring_interval_args);
                                }
                            }
                        }

                    } catch (\ChamaWP\Vendor\Stripe\ApiErrorException $e) {

                        add_action('admin_notices', function () use ($notification) {
                            echo '<div class="notice-error notice"><p>', esc_html($e->getError()->message), '</p></div>';
                        });
                    } catch (Exception $e) {

                        add_action('admin_notices', function () use ($notification) {
                            echo '<div class="notice-error notice"><p>', esc_html($e->getMessage()), '</p></div>';
                        });
                    }
                }
            }

        }
    endif;
    if (! function_exists('chama_add_price')):
        function chama_add_price($post_id, $product_id, $tier_price_cents, $recurring_interval_args)
    {
            $secret_key = chama_is_live_mode() ? CHAMA_STRIPE_LIVE_SECRET_KEY : CHAMA_STRIPE_TEST_SECRET_KEY;

            if ($secret_key) {
                $stripe = new \ChamaWP\Vendor\Stripe\StripeClient(
                    $secret_key // API Secret here
                );
                $price_response = $stripe->prices->create([
                    'product'     => $product_id,
                    'currency'    => CHAMA_CURRENCY,
                    'unit_amount' => $tier_price_cents,
                    'recurring'   => $recurring_interval_args,
                ]);
                if ($price_response->id) {
                    $price_id = $price_response->id;
                    update_post_meta($post_id, 'tier_stripe_price_id', $price_id);
                }
                $stripe->products->update(
                    $product_id,
                    ['default_price' => $price_id]
                );
            }

        }
    endif;
    //on delete tier
    add_action('before_delete_post', 'chama_delete_tier_from_stripe', 10);
    function chama_delete_tier_from_stripe($post_id)
    {
        try {
            global $post_type;
            if ($post_type !== 'chama_tier') {
                return;
            }

            $secret_key = chama_is_live_mode() ? CHAMA_STRIPE_LIVE_SECRET_KEY : CHAMA_STRIPE_TEST_SECRET_KEY;

            if ($secret_key) {
                $stripe = new \ChamaWP\Vendor\Stripe\StripeClient(
                    $secret_key // API Secret here
                );
                //delete previous price
                $stripe->products->update(
                    get_post_meta($post_id, 'tier_stripe_product_id', true),
                    ['default_price' => '']
                );
                $deletion_args = [
                    'active' => false,
                ];
                if (get_post_meta($post_id, 'tier_stripe_price_id', true)) {
                    $stripe->prices->update(
                        get_post_meta($post_id, 'tier_stripe_price_id', true),
                        $deletion_args
                    );
                }
                if (get_post_meta($post_id, 'tier_stripe_product_id', true)) {
                    //$stripe->products->delete(get_post_meta($post_id, 'tier_stripe_product_id', true), []);
                    $stripe->products->update(
                        get_post_meta($post_id, 'tier_stripe_product_id', true),
                        $deletion_args
                    );
                }
            }
        } catch (\ChamaWP\Vendor\Stripe\ApiErrorException $e) {

            add_action('admin_notices', function () use ($notification) {
                echo '<div class="notice-error notice"><p>', esc_html($e->getError()->message), '</p></div>';
            });
        } catch (Exception $e) {

            add_action('admin_notices', function () use ($notification) {
                echo '<div class="notice-error notice"><p>', esc_html($e->getMessage()), '</p></div>';
            });
        }

    }
    //show columns
    add_filter('manage_chama_tier_posts_columns', 'chama_tier_columns');
    function chama_tier_columns($columns)
    {
        $columns = [
            'cb'          => $columns['cb'],
            'title'       => __('Title', 'chama'),
            'limit'       => __('Limit', 'chama'),
            'price'       => __('Price', 'chama'),
            'highlighted' => __('Highlighted', 'chama'),
            'date'        => __('Date', 'chama'),
        ];
        return $columns;
    }
    add_filter('manage_chama_tier_posts_custom_column', 'chama_populate_tier_columns', 10, 2);
    function chama_populate_tier_columns($column, $id)
    {
        switch ($column) {
            case 'limit':
                $limit = get_post_meta($id, 'member_limit', true);

                echo intval($limit);

                break;
            case 'price':
                $price = get_post_meta($id, 'tier_price', true);
                echo esc_html(CHAMA_SYMBOL . number_format((float) $price, 2, '.', ''));
                break;
            case 'highlighted':
                $is_highlighted = get_post_meta($id, 'tier_is_highlighted', true);
                if (! empty($is_highlighted)) {
                    echo esc_html($is_highlighted);
                } else {
                    echo 'no';
                }
                break;
            default:
                break;
        }
    }

    /*=============================================CAMPAIGN=============================================*/
    /**
     * Add Campaign metaboxes
     */
    add_action('save_post_chama_campaign', 'chama_campaign_save_postdata', 20, 2);
    //initializing meta box
    add_action('admin_init', 'chama_campaign_description_init_metabox');
    if (! function_exists('chama_campaign_description_init_metabox')):
        function chama_campaign_description_init_metabox()
    {
            add_meta_box(
                'campaign_description',
                __('Goal Description', 'chama'),
                'chama_campaign_description_display_metabox',
                'chama_campaign',
                "normal",
                "default"
            );

        }
    endif;
    //Displaying the meta box
    if (! function_exists('chama_campaign_description_display_metabox')):
        function chama_campaign_description_display_metabox($post)
    {
            chama_textarea_display_metabox($post->ID, 'campaign_description', 'Give a short description of why you need to reach your goal. Description cannot be longer than 500 characters.', 500);

        }
    endif;
    //Youtube URL
    //initializing meta box
    add_action('admin_init', 'chama_campaign_youtube_url_init_metabox');
    if (! function_exists('chama_campaign_youtube_url_init_metabox')):
        function chama_campaign_youtube_url_init_metabox()
    {
            add_meta_box(
                'campaign_youtube_url',
                __('Introduction Video', 'chama'),
                'chama_campaign_youtube_url_display_metabox',
                'chama_campaign',
                "normal",
                "default"
            );

        }
    endif;
    //Displaying the meta box
    if (! function_exists('chama_campaign_youtube_url_display_metabox')):
        function chama_campaign_youtube_url_display_metabox($post)
    {
            chama_youtube_url_display_metabox($post->ID, 'campaign_youtube_url', 'Youtube Video ID e.g. https://www.youtube.com/embed/g3nUV_y648. This is a video briefly describing the reason for the campaign.');

        }
    endif;
    //Campaign FAQs
    add_action('admin_init', 'chama_campaign_faqs_init_metabox', 99);
    if (! function_exists('chama_campaign_faqs_init_metabox')):
        function chama_campaign_faqs_init_metabox()
    {
            add_meta_box(
                'campaign_faqs',
                __('Frequently Asked Questions', 'chama'),
                'chama_campaign_faqs_display_metabox',
                'chama_campaign',
                "normal",
                "default"
            );

        }
    endif;
    //Displaying the meta box
    if (! function_exists('chama_campaign_faqs_display_metabox')):
        function chama_campaign_faqs_display_metabox($post)
    {
            $faqs = get_post_meta($post->ID, 'campaign_faqs', true);
        ?>

																																																																																																																													  <table id="campaign-faqs-repeater" class="chama-repeater-table" width="100%">
																																																																																																																													  <tbody>
																																																																																																																												        <tr>
																																																																																																																												            <th>
																																																																																																																												            Question
																																																																																																																												            </th>
																																																																																																																												            <th>
																																																																																																																												            Answer
																																																																																																																												                </th>
																																																																																																																												                <th>

																																																																																																																												                </th>
																																																																																																																												        </tr>
																																																																																																																													    <?php
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if ($faqs):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        foreach ($faqs as $field) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ?>
																																																																																																																																																																																																																																																										    <tr>
																																																																																																																																																																																																																																																										      <td width="45%">
																																																																																																																																																																																																																																																										        <input type="text"  placeholder="Question" title="Question" name="campaign_faq_question[]" value="<?php if ($field['campaign_faq_question'] != '') {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          echo esc_attr($field['campaign_faq_question']);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  ?>" class="widefat" /></td>
																																																																																																																																																																																																																																																										      <td width="45%">
																																																																																																																																																																																																																										                                                                                                                                      <textarea class="widefat"  name="campaign_faq_answer[]" maxlength="3000" cols="40" rows="4" style="min-height: 120px;"><?php echo $field['campaign_faq_answer'] != '' ? esc_html($field['campaign_faq_answer']) : ''; ?></textarea>

																																																																																																																																																																																																																																																										     </td>
																																																																																																																																																																																																																																																										      <td width="10%"><a class="button remove-faq-row" href="#1">Remove</a></td>
																																																																																																																																																																																																																																																										    </tr>
																																																																																																																																																																																																																																																										    <?php
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        } else :
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            // show a blank one
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ?>
																																																																																																																																																																																																																																																										    <tr>
																																																																																																																																																																																																																																																										    <td width="45%">
																																																																																																																																																																																																																																																										        <input type="text" placeholder="Question" title="Question" name="campaign_faq_question[]" class="widefat" /></td>
																																																																																																																																																																																																																																																										        <td width="45%">
																																																																																																																																																																																																																										                                                                                                                                        <textarea class="widefat"  name="campaign_faq_answer[]" maxlength="3000" cols="40" rows="4" style="min-height: 120px;"></textarea>


																																																																																																																																																																																																																																																										          </td>
																																																																																																																																																																																																																																																										          <td width="10%"><a class="button  cmb-remove-row-button button-disabled" href="#">Remove</a></td>
																																																																																																																																																																																																																																																										    </tr>
																																																																																																																																																																																																																																																										    <?php endif; ?>

																																																																																																																													    <!-- empty hidden one for jQuery -->
																																																																																																																													    <tr class="faq-empty-row screen-reader-text">
																																																																																																																													    <td width="45%">
																																																																																																																													    <input type="text" placeholder="Question" title="Question" name="campaign_faq_question[]" class="widefat" /></td>
																																																																																																																													        <td width="45%">
																																																																																																																													        <textarea class="widefat"  name="campaign_faq_answer[]" maxlength="3000" cols="40" rows="4" style="min-height: 120px;"></textarea>
																																																																																																																													          </td>
																																																																																																																													          <td width="10%"><a class="button remove-faq-row" href="#">Remove</a></td>
																																																																																																																													    </tr>
																																																																																																																													  </tbody>
																																																																																																																													</table>
																																																																																																																													<p><a id="add-faq-row" class="button" href="#">Add another</a></p>
																																																																																																																													 <?php
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //Campaign Updates
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         add_action('admin_init', 'chama_campaign_updates_init_metabox', 100);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if (! function_exists('chama_campaign_updates_init_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             function chama_campaign_updates_init_metabox()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 add_meta_box(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'campaign_updates',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     __('Campaign Updates', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'chama_campaign_updates_display_metabox',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'chama_campaign',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "normal",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "default"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 );

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //Displaying the meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if (! function_exists('chama_campaign_updates_display_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             function chama_campaign_updates_display_metabox($post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 $updates = get_post_meta($post->ID, 'campaign_updates', true);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ?>

																																																																																																																													  <table id="campaign-updates-repeater" class="chama-repeater-table" width="100%">
																																																																																																																													  <tbody>
																																																																																																																												        <tr>
																																																																																																																												            <th>
																																																																																																																												            Date
																																																																																																																												            </th>
																																																																																																																												            <th>
																																																																																																																												            Content
																																																																																																																												                </th>
																																																																																																																												                <th>

																																																																																																																												                </th>
																																																																																																																												        </tr>
																																																																																																																													    <?php
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if ($updates):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        foreach ($updates as $field) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $formatted_date  = new DateTime($field['campaign_update_date']);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $dateWithoutTime = $formatted_date->format('Y-m-d');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ?>
																																																																																																																																																																																																																																																										    <tr>
																																																																																																																																																																																																																																																										      <td width="45%">
																																																																																																																																																																																																																						                                                                                                                                                      <input type="date" name="campaign_update_date[]"  value="<?php if ($field['campaign_update_date'] != '') {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               echo esc_attr($dateWithoutTime);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ?>" class="widefat" size="30" />
																																																																																																																																																																																																																																																										        </td>
																																																																																																																																																																																																																																																										      <td width="45%">
																																																																																																																																																																																																																										                                                                                                                                      <textarea class="widefat"  name="campaign_update_content[]" maxlength="3000" cols="40" rows="4" style="min-height: 120px;"><?php echo $field['campaign_update_content'] != '' ? esc_html($field['campaign_update_content']) : ''; ?></textarea>

																																																																																																																																																																																																																																																										     </td>
																																																																																																																																																																																																																																																										      <td width="10%"><a class="button remove-update-row" href="#1">Remove</a></td>
																																																																																																																																																																																																																																																										    </tr>
																																																																																																																																																																																																																																																										    <?php
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        } else :
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            // show a blank one
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ?>
																																																																																																																																																																																																																																																										    <tr>
																																																																																																																																																																																																																																																										    <td width="45%">
																																																																																																																																																																																																																																																										        <input type="date" title="Date" name="campaign_update_date[]" class="widefat" /></td>
																																																																																																																																																																																																																																																										        <td width="45%">
																																																																																																																																																																																																																										                                                                                                                                        <textarea class="widefat"  name="campaign_update_content[]" maxlength="3000" cols="40" rows="4" style="min-height: 120px;"></textarea>


																																																																																																																																																																																																																																																										          </td>
																																																																																																																																																																																																																																																										          <td width="10%"><a class="button  cmb-remove-row-button button-disabled" href="#">Remove</a></td>
																																																																																																																																																																																																																																																										    </tr>
																																																																																																																																																																																																																																																										    <?php endif; ?>

																																																																																																																													    <!-- empty hidden one for jQuery -->
																																																																																																																													    <tr class="update-empty-row screen-reader-text">
																																																																																																																													    <td width="45%">
																																																																																																											                                                                            <input type="date" title="Date" name="campaign_update_date[]" class="widefat" /></td>
																																																																																																																													        <td width="45%">
																																																																																																																													        <textarea class="widefat"  name="campaign_update_content[]" maxlength="3000" cols="40" rows="4" style="min-height: 120px;"></textarea>
																																																																																																																													          </td>
																																																																																																																													          <td width="10%"><a class="button remove-update-row" href="#">Remove</a></td>
																																																																																																																													    </tr>
																																																																																																																													  </tbody>
																																																																																																																													</table>
																																																																																																																													<p><a id="add-update-row" class="button" href="#">Add another</a></p>
																																																																																																																													 <?php
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //End date
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //initializing meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         add_action('admin_init', 'chama_campaign_end_date_init_metabox');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if (! function_exists('chama_campaign_end_date_init_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             function chama_campaign_end_date_init_metabox()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 add_meta_box(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'campaign_end_date',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     __('End Date', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'chama_campaign_end_date_display_metabox',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'chama_campaign',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "side",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "low"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 );

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //Displaying the meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if (! function_exists('chama_campaign_end_date_display_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             function chama_campaign_end_date_display_metabox($post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 chama_date_display_metabox($post->ID, 'campaign_end_date', 'Enter the date on which this campaign will end. Supporters will not be able to fund the campaign after this date.');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //Target amount
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         add_action('admin_init', 'chama_campaign_target_amount_init_metabox');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //initializing meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if (! function_exists('chama_campaign_target_amount_init_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             function chama_campaign_target_amount_init_metabox()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 add_meta_box(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'campaign_target_amount',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     __('Target Amount', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'chama_campaign_target_amount_display_metabox',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'chama_campaign',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "side",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "low"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 );

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //Displaying the meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if (! function_exists('chama_campaign_target_amount_display_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             function chama_campaign_target_amount_display_metabox($post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 chama_price_display_metabox($post->ID, 'campaign_target_amount', 'Set an amount you wish to target for the campaign. Goal progess will be shown as a percentage.');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //Starting amount
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         add_action('admin_init', 'chama_campaign_starting_amount_init_metabox');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if (! function_exists('chama_campaign_starting_amount_init_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             function chama_campaign_starting_amount_init_metabox()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 add_meta_box(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'campaign_starting_amount',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     __('Starting Amount', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'chama_campaign_starting_amount_display_metabox',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'chama_campaign',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "side",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "low"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 );

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //Displaying the meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if (! function_exists('chama_campaign_starting_amount_display_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             function chama_campaign_starting_amount_display_metabox($post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 chama_price_display_metabox($post->ID, 'campaign_starting_amount', 'You may want to include income you have already received. Or you may just not want to start from 0%.  Enter an amount, you wish to start the campaign at.');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //Minimum campaign donation
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         add_action('admin_init', 'chama_campaign_min_donation_init_metabox');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if (! function_exists('chama_campaign_min_donation_init_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             function chama_campaign_min_donation_init_metabox()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 add_meta_box(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'campaign_min_donation',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     __('Minimum Donation', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'chama_campaign_min_donation_display_metabox',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'chama_campaign',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "side",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "low"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 );

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //Displaying the meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if (! function_exists('chama_campaign_min_donation_display_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             function chama_campaign_min_donation_display_metabox($post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 chama_price_display_metabox($post->ID, 'campaign_min_donation', 'Set the minimum donation that can be paid for this campaign. Supporters can choose to pay more.');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //Target amount publicly
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         add_action('admin_init', 'chama_campaign_show_target_amount_init_metabox');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if (! function_exists('chama_campaign_show_target_amount_init_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             function chama_campaign_show_target_amount_init_metabox()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 add_meta_box(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'campaign_show_target_amount',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     __('Display Target Amount', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'chama_campaign_show_target_amount_display_metabox',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'chama_campaign',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "side",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "low"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 );

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //Displaying the meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if (! function_exists('chama_campaign_show_target_amount_display_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             function chama_campaign_show_target_amount_display_metabox($post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 chama_checkbox_display_metabox($post->ID, 'campaign_show_target_amount', 'Show the total target amount on your crowdfunding page.', 'Show target amount publicly');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //Campaign analytics
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         add_action('admin_init', 'chama_campaign_analytics_init_metabox');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if (! function_exists('chama_campaign_analytics_init_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             function chama_campaign_analytics_init_metabox()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 add_meta_box(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'campaign_analytics',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     __('Analytics', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'chama_campaign_analytics_display_metabox',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'chama_campaign',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "side",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "low"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 );

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //Displaying the meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if (! function_exists('chama_campaign_analytics_display_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             function chama_campaign_analytics_display_metabox($post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 $current_total_backers = get_post_meta($post->ID, 'campaign_current_total_backers', true) ? get_post_meta($post->ID, 'campaign_current_total_backers', true) : 0;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 $current_total_amount  = get_post_meta($post->ID, 'campaign_current_total_amount', true) ? get_post_meta($post->ID, 'campaign_current_total_amount', true) : 0;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 echo '<strong>Current total backers:</strong> ' . esc_html($current_total_backers) . '<br/>';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 echo '<strong>Current total amount:</strong> ' . esc_html($current_total_amount);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //This function saves the data you put in the meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if (! function_exists('chama_campaign_save_postdata')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             function chama_campaign_save_postdata($post_id, $post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 if ($post->post_type == 'chama_campaign') {

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     //Not save if the user hasn't submitted changes
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     // Verifying whether input is coming from the proper form
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     if (
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     ! isset($_POST['chama_campaign_nonce']) ||
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     ! wp_verify_nonce(sanitize_key(wp_unslash($_POST['chama_campaign_nonce'])), 'campaign_metadata')
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 ) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     // Making sure the user has permission

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     if (! current_user_can('edit_post', $post_id)) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     if (! empty($_POST['campaign_description'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         /* Get the posted data and sanitize it for use as an HTML class. */
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         $data = sanitize_text_field(wp_unslash($_POST['campaign_description']));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         update_post_meta($post_id, 'campaign_description', $data);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         delete_post_meta($post_id, 'campaign_description');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     if (! empty($_POST['campaign_end_date'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         /* Get the posted data and sanitize it for use as an HTML class. */

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         $raw_date = sanitize_text_field(wp_unslash($_POST['campaign_end_date']));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         $data     = chama_sanitize_date($raw_date);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         $formatted_date = strtotime($data, 0);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         $formatted_date = gmdate('Y-m-d H:i:s', $formatted_date);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         update_post_meta($post_id, 'campaign_end_date', $formatted_date);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         update_post_meta($post_id, 'campaign_end_date', false);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     if (! empty($_POST['campaign_target_amount'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         /* Get the posted data and sanitize it for use as an HTML class. */
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         $data = str_replace(',', '', number_format(floatval($_POST['campaign_target_amount']), 2));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         update_post_meta($post_id, 'campaign_target_amount', $data);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         update_post_meta($post_id, 'campaign_target_amount', 0);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     if (! empty($_POST['campaign_starting_amount'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         /* Get the posted data and sanitize it for use as an HTML class. */
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         $data = str_replace(',', '', number_format(floatval($_POST['campaign_starting_amount']), 2));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         update_post_meta($post_id, 'campaign_starting_amount', $data);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         update_post_meta($post_id, 'campaign_starting_amount', 0);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     if (! empty($_POST['campaign_min_donation'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         /* Get the posted data and sanitize it for use as an HTML class. */
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         $data = str_replace(',', '', number_format(floatval($_POST['campaign_min_donation']), 2));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         update_post_meta($post_id, 'campaign_min_donation', $data);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         update_post_meta($post_id, 'campaign_min_donation', 0);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     if (! empty($_POST['campaign_show_target_amount'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         /* Get the posted data and sanitize it for use as an HTML class. */
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         $data = sanitize_text_field(wp_unslash($_POST['campaign_show_target_amount']));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if ($data == "yes") {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             $campaign_show_target_amount = "yes";
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             $campaign_show_target_amount = "no";
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         update_post_meta($post_id, 'campaign_show_target_amount', $campaign_show_target_amount);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         delete_post_meta($post_id, 'campaign_show_target_amount');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     if (! empty($_POST['campaign_youtube_url'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         /* Get the posted data and sanitize it for use as an HTML class. */
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         $data = sanitize_text_field(wp_unslash($_POST['campaign_youtube_url']));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         update_post_meta($post_id, 'campaign_youtube_url', $data);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         delete_post_meta($post_id, 'campaign_youtube_url');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     if (! empty($_POST['campaign_faq_question']) && ! empty($_POST['campaign_faq_answer'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         $current_faqs = get_post_meta($post_id, 'campaign_faqs', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         $new_faqs     = [];

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         $faq_questons = array_map('sanitize_text_field', wp_unslash($_POST['campaign_faq_question']));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         $faq_answers  = array_map('sanitize_text_field', wp_unslash($_POST['campaign_faq_answer']));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         $count        = count($faq_questons);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         for ($i = 0; $i < $count; $i++) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             if ($faq_questons[$i] != ''):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 $new_faqs[$i]['campaign_faq_question'] = sanitize_text_field(stripslashes(wp_strip_all_tags($faq_questons[$i])));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 $new_faqs[$i]['campaign_faq_answer']   = sanitize_textarea_field(stripslashes(wp_strip_all_tags($faq_answers[$i])));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if (! empty($new_faqs) && $new_faqs != $current_faqs) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             update_post_meta($post_id, 'campaign_faqs', $new_faqs);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         } elseif (empty($new_faqs) && $current_faqs) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         delete_post_meta($post_id, 'campaign_faqs', $current_faqs);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     update_post_meta($post_id, 'campaign_faqs', 0);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 if (! empty($_POST['campaign_update_date']) && ! empty($_POST['campaign_update_content'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     $current_updates = get_post_meta($post_id, 'campaign_updates', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     $new_updates     = [];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     $update_date     = array_map('sanitize_text_field', wp_unslash($_POST['campaign_update_date']));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     $update_content  = array_map('sanitize_text_field', wp_unslash($_POST['campaign_update_content']));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     $count           = count($update_date);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     for ($i = 0; $i < $count; $i++) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if ($update_date[$i] != ''):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             $new_updates[$i]['campaign_update_date']    = chama_sanitize_date(stripslashes(wp_strip_all_tags($update_date[$i])));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             $new_updates[$i]['campaign_update_content'] = sanitize_text_field($update_content[$i]);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     if (! empty($new_updates) && $new_updates != $current_updates) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         update_post_meta($post_id, 'campaign_updates', $new_updates);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     } elseif (empty($new_updates) && $current_updates) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         delete_post_meta($post_id, 'campaign_updates', $current_updates);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     update_post_meta($post_id, 'campaign_updates', 0);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 chama_flush_permalinks();

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //show columns
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         add_filter('manage_chama_campaign_posts_columns', 'chama_campaign_columns');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         function chama_campaign_columns($columns)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             $columns = [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 'cb'          => $columns['cb'],
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 'title'       => __('Title', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 'target'      => __('Target Amount', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 'start'       => __('Starting Amount', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 'min'         => __('Minimum Amount', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 'show_target' => __('Show Target Amount', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 'end_date'    => __('End Date', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 'date'        => __('Date', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             return $columns;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         add_filter('manage_chama_campaign_posts_custom_column', 'chama_populate_campaign_columns', 10, 2);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         function chama_populate_campaign_columns($column, $id)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             switch ($column) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 case 'target':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     $target = get_post_meta($id, 'campaign_target_amount', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     echo esc_html(CHAMA_SYMBOL . number_format((float) $target, 2, '.', ''));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 case 'start':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     $start = get_post_meta($id, 'campaign_starting_amount', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     echo esc_html(CHAMA_SYMBOL . number_format((float) $start, 2, '.', ''));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 case 'min':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     $min = get_post_meta($id, 'campaign_min_donation', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     echo esc_html(CHAMA_SYMBOL . number_format((float) $min, 2, '.', ''));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 case 'show_target':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     $show_target = get_post_meta($id, 'campaign_show_target_amount', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     if (! empty($show_target)) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         echo esc_html($show_target);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         echo 'no';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 case 'end_date':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     $end_date           = get_post_meta($id, 'campaign_end_date', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     $end_date_formatted = gmdate("F, j, Y", strtotime($end_date));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     if (! empty($end_date)) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         echo esc_html($end_date_formatted);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 default:
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //Extend search for campaigns(description)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         add_action('admin_init', 'chama_campaign_extend_search');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         function chama_campaign_extend_search()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             global $typenow;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             if ($typenow === 'chama_campaign') {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 add_filter('posts_search', 'chama_campaign_search_override', 10, 2);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         function chama_campaign_search_override($search, $query)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             // Override the campaigns page search to include custom meta data

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             global $wpdb;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             if ($query->is_main_query() && ! empty($query->query['s'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 $like   = '%' . $wpdb->esc_like($query->query['s']) . '%';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 $search = preg_replace(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "#\({$wpdb->posts}.post_title LIKE [^)]+\)\K#",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     $wpdb->prepare("
or exists (
select * from {$wpdb->postmeta} where post_id={$wpdb->posts}.ID
and meta_key in ('campaign_description')
and meta_value like %s
)
", $like),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     $search
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 );
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             return $search;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         /*=============================================COMMISSION=============================================*/
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         /**
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          * Add Commission metaboxes
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          */

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //commission price
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         add_action('save_post_chama_commission', 'chama_commission_save_postdata', 20, 2);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         add_action('admin_init', 'chama_commission_price_init_metabox');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if (! function_exists('chama_commission_price_init_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             function chama_commission_price_init_metabox()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 add_meta_box(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'commission_price',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     __('Price', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'chama_commission_price_display_metabox',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'chama_commission',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "side",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "low"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 );

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //Displaying the meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if (! function_exists('chama_commission_price_display_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             function chama_commission_price_display_metabox($post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 chama_price_display_metabox($post->ID, 'commission_price', 'The minimum price is
																																																																																																																													');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         endif;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //Enable 'Pay What You Want'
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         add_action('admin_init', 'chama_commission_pay_what_you_want_init_metabox');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if (! function_exists('chama_commission_pay_what_you_want_init_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             function chama_commission_pay_what_you_want_init_metabox()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 add_meta_box(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'commission_pay_what_you_want',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     __('Pay What You Want Price', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'chama_commission_pay_what_you_want_display_metabox',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'chama_commission',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "side",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "low"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 );

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //Displaying the meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if (! function_exists('chama_commission_pay_what_you_want_display_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             function chama_commission_pay_what_you_want_display_metabox($post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 chama_checkbox_display_metabox($post->ID, 'commission_pay_what_you_want', 'If you enable \'Pay What You Want\', your price becomes the minimum price someone can pay for the commission.', 'Enable \'Pay What You Want\'');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //Commission description
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         add_action('admin_init', 'chama_commission_description_init_metabox', 2);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if (! function_exists('chama_commission_description_init_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             function chama_commission_description_init_metabox()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 add_meta_box(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'commission_description',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     __('Description', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'chama_commission_description_display_metabox',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'chama_commission',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "normal",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "default"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 );

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //Displaying the meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if (! function_exists('chama_commission_description_display_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             function chama_commission_description_display_metabox($post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 chama_textarea_display_metabox($post->ID, 'commission_description', 'Give a clear description of what the buyer can expect.', 5000);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //Commission images
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         add_action('admin_init', 'chama_commission_preview_images_init_metabox', 2);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if (! function_exists('chama_commission_preview_images_init_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             function chama_commission_preview_images_init_metabox()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 add_meta_box(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'commission_preview_images',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     __('Preview images', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'chama_commission_preview_images_display_metabox',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'chama_commission',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "normal",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "default"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 );

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //Displaying the meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if (! function_exists('chama_commission_preview_images_display_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             function chama_commission_preview_images_display_metabox($post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ?>
																																																																																																																								    <ul class="chama-commission-gallery chama-gallery-input">
																																																																																																																									<?php

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $image_ids = ($image_ids = get_post_meta($post->ID, 'commission_preview_images', true)) ? array_filter($image_ids) : [];

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                foreach ($image_ids as $i => &$id) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $url = wp_get_attachment_image_url($id, [140, 140]);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    if ($url) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ?>
																																																																																																																													<li data-id="<?php echo esc_attr($id) ?>">
																																																																																																																														<span class="chama-preview-image" style="background-image:url('<?php echo esc_url($url); ?>')"></span>
																																																																																																																								                        <a class="button chama-commission-gallery-remove" href="#" data-hidden="commission_preview_images" data-id="<?php echo esc_attr($id) ?>">Remove</a>
																																																																																																																													</li>
																																																																																																																												<?php
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    unset($image_ids[$i]);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ?>
																																																																																																																								</ul>
																																																																																																																								<input type="hidden" id="commission_preview_images" name="commission_preview_images" value="<?php echo esc_attr(join(',', $image_ids)); ?>" />
																																																																																																																								<a href="#" class="button chama-gallery-upload-button">Add Images</a>
																																																																																																																								    <?php
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        //Add ons
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        add_action('admin_init', 'chama_commission_add_ons_init_metabox', 2);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if (! function_exists('chama_commission_add_ons_init_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_commission_add_ons_init_metabox()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                add_meta_box(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'commission_add_ons',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    __('Add-ons', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'chama_commission_add_ons_display_metabox',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'chama_commission',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    "normal",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    "default"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                );

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        //Displaying the meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if (! function_exists('chama_commission_add_ons_display_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_commission_add_ons_display_metabox($post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $add_ons = get_post_meta($post->ID, 'commission_add_ons', true);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ?>

																																																																																																																													  <table id="commission-add-ons-repeater" class="chama-repeater-table" width="100%">
																																																																																																																													  <tbody>
																																																																																																																												        <tr>
																																																																																																																												            <th>
																																																																																																																												            Name
																																																																																																																												            </th>
																																																																																																																												            <th>
																																																																																																																												            Price
																																																																																																																												                </th>
																																																																																																																												                <th>

																																																																																																																												                </th>
																																																																																																																												        </tr>
																																																																																																																													    <?php
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if ($add_ons):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        foreach ($add_ons as $field) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ?>
																																																																																																																																																																																																																																																										    <tr>
																																																																																																																																																																																																																																																										      <td width="45%">
																																																																																																																																																																																																																																																										        <input type="text"  placeholder="Option" title="Option" name="commission_add_on_name[]" value="<?php if ($field['commission_add_on_name'] != '') {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       echo esc_attr($field['commission_add_on_name']);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               ?>" class="widefat" /></td>
																																																																																																																																																																																																																																																										      <td width="45%">
																																																																																																																																																																																																																																																										      <div class="chama-input-wrapper">
																																																																																																																																																																																																																																																										      <div class="chama-input-prepend">
																																																																																																																																																																																																																																																												                                    <span class="chama-input-prepend-text">

																																																																																																																																																																																																																																																												                        <?php echo esc_html(CHAMA_SYMBOL); ?>            </span>
																																																																																																																																																																																																																																																												                                </div>
																																																																																																																																																																																																																																																												                <input class="widefat chama-input" type="number" min="0" step="any" name="commission_add_on_price[]" value="<?php echo esc_attr(empty($field['commission_add_on_price']) ? 0 : $field['commission_add_on_price']); ?>" size="30" />
																																																																																																																																																																																																																																																												                </div>
																																																																																																																																																																																																																																																										     </td>
																																																																																																																																																																																																																																																										      <td width="10%"><a class="button remove-add-on-row" href="#1">Remove</a></td>
																																																																																																																																																																																																																																																										    </tr>
																																																																																																																																																																																																																																																										    <?php
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        } else :
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            // show a blank one
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ?>
																																																																																																																																																																																																																																																										    <tr>
																																																																																																																																																																																																																																																										    <td width="45%">
																																																																																																																																																																																																																																																										        <input type="text" placeholder="Option" title="Option" name="commission_add_on_name[]" class="widefat" /></td>
																																																																																																																																																																																																																																																										        <td width="45%">
																																																																																																																																																																																																																																																										        <div class="chama-input-wrapper">
																																																																																																																																																																																																																																																										        <div class="chama-input-prepend">
																																																																																																																																																																																																																																																												                                    <span class="chama-input-prepend-text">

																																																																																																																																																																																																																																																												                        <?php echo esc_html(CHAMA_SYMBOL); ?>            </span>
																																																																																																																																																																																																																																																												                                </div>
																																																																																																																																																																																																																																																												                <input class="widefat chama-input" type="number" min="0" step="any" name="commission_add_on_price[]" value="0" size="30" />
																																																																																																																																																																																																																																																												                </div>

																																																																																																																																																																																																																																																										          </td>
																																																																																																																																																																																																																																																										          <td width="10%"><a class="button  cmb-remove-row-button button-disabled" href="#">Remove</a></td>
																																																																																																																																																																																																																																																										    </tr>
																																																																																																																																																																																																																																																										    <?php endif; ?>

																																																																																																																													    <!-- empty hidden one for jQuery -->
																																																																																																																													    <tr class="add-on-empty-row screen-reader-text">
																																																																																																																													    <td width="45%">
																																																																																																																													    <input type="text" placeholder="Option" title="Option" name="commission_add_on_name[]" class="widefat" /></td>
																																																																																																																													        <td width="45%">
																																																																																																																													        <div class="chama-input-wrapper">
																																																																																																																													        <div class="chama-input-prepend">
																																																																																																																															                                    <span class="chama-input-prepend-text">

																																																																																																																															                        <?php echo esc_html(CHAMA_SYMBOL); ?>            </span>
																																																																																																																															                                </div>
																																																																																																																															                <input class="widefat chama-input" type="number" min="0" step="any" name="commission_add_on_price[]" value="0" size="30" />
																																																																																																																															                </div>
																																																																																																																													          </td>
																																																																																																																													          <td width="10%"><a class="button remove-add-on-row" href="#">Remove</a></td>
																																																																																																																													    </tr>
																																																																																																																													  </tbody>
																																																																																																																													</table>
																																																																																																																													<p><a id="add-add-on-row" class="button" href="#">Add another</a></p>
																																																																																																																													 <?php
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //Quantity limit

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         add_action('admin_init', 'chama_commission_limit_init_metabox');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if (! function_exists('chama_commission_limit_init_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             function chama_commission_limit_init_metabox()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 add_meta_box(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'commission_limit',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     __('Quantity Available', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'chama_commission_limit_display_metabox',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'chama_commission',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "side",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "low"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 );

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //Displaying the meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if (! function_exists('chama_commission_limit_display_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             function chama_commission_limit_display_metabox($post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 chama_number_display_metabox($post->ID, 'commission_limit', 'Limit the total number of slots that can be sold. Set to \'0\' (zero) for sold out. Set to \'-1\' (minus one) for no limit.');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //Who can request commission
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         add_action('admin_init', 'chama_commission_can_request_init_metabox');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if (! function_exists('chama_commission_can_request_init_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             function chama_commission_can_request_init_metabox()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 add_meta_box(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'commission_can_request',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     __('Who can request this item', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'chama_commission_can_request_display_metabox',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'chama_commission',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "side",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "low"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 );

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //Displaying the meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if (! function_exists('chama_commission_can_request_display_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             function chama_commission_can_request_display_metabox($post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 $limit = get_post_meta($post->ID, 'commission_can_request', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ?>
																																																																																																																												    <select name="commission_can_request" id="commission_can_request" class="widefat" >
																																																																																																																												        <option value="no"																																																																																																																												                          																																																																																																																											                          																																																																																																																										                          																																																																																																																									                          																																																																																																																								                          																																																																																																																							                          																																																																																																																						                          																																																																																																																					                          																																																																																																																				                          																																																																																																																			                          																																																																																																																		                          																																																																																																																	                          																																																																																																																                          																																																																																																															                          																																																																																																														                          																																																																																																													                          																																																																																																												                          																																																																																																											                          																																																																																																										                          																																																																																																									                          																																																																																																								                          																																																																																																							                          																																																																																																						                          																																																																																																					                          																																																																																																				                          																																																																																																			                          																																																																																																		                          																																																																																																	                          																																																																																																                          																																																																																															                          																																																																																														                          																																																																																													                          																																																																																												                          																																																																																											                          																																																																																										                          																																																																																									                          																																																																																								                          																																																																																							                          																																																																																						                          																																																																																					                          																																																																																				                          																																																																																			                          																																																																																		                          																																																																																	                          																																																																																                          																																																																															                          																																																																														                          																																																																													                          																																																																												                          																																																																											                          																																																																										                          																																																																									                           <?php selected('no', $limit); ?>>Monthly Supporters</option>
																																																																																																																												        <option value="yes"																																																																																																																												                           																																																																																																																											                           																																																																																																																										                           																																																																																																																									                           																																																																																																																								                           																																																																																																																							                           																																																																																																																						                           																																																																																																																					                           																																																																																																																				                           																																																																																																																			                           																																																																																																																		                           																																																																																																																	                           																																																																																																																                           																																																																																																															                           																																																																																																														                           																																																																																																													                           																																																																																																												                           																																																																																																											                           																																																																																																										                           																																																																																																									                           																																																																																																								                           																																																																																																							                           																																																																																																						                           																																																																																																					                           																																																																																																				                           																																																																																																			                           																																																																																																		                           																																																																																																	                           																																																																																																                           																																																																																															                           																																																																																														                           																																																																																													                           																																																																																												                           																																																																																											                           																																																																																										                           																																																																																									                           																																																																																								                           																																																																																							                           																																																																																						                           																																																																																					                           																																																																																				                           																																																																																			                           																																																																																		                           																																																																																	                           																																																																																                           																																																																															                           																																																																														                           																																																																													                           																																																																												                           																																																																											                           																																																																										                           																																																																									                            <?php selected('yes', $limit); ?>>Everyone</option>
																																																																																																																												    </select>
																																																																																																																												    <?php
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        //Shipping options
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        add_action('admin_init', 'chama_commission_shipping_options_init_metabox', 2);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if (! function_exists('chama_commission_shipping_options_init_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_commission_shipping_options_init_metabox()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                add_meta_box(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'commission_shipping_options',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    __('Shipping options', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'chama_commission_shipping_options_display_metabox',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'chama_commission',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    "normal",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    "default"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                );

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        //Displaying the meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if (! function_exists('chama_commission_shipping_options_display_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_commission_shipping_options_display_metabox($post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $locations                 = chama_get_stripe_locations();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $unselected_locations_args = [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'name'           => 'commission_shipping_location[]',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'select_options' => $locations,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'disabled_label' => 'Country',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'value'          => "-1",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ];

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $shipping_prices = get_post_meta($post->ID, 'commission_shipping_prices', true);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ?>
																																																																																																																													  <table id="commission-shipping-prices-repeater" class="chama-repeater-table" width="100%">
																																																																																																																													  <tbody>
																																																																																																																												        <tr>
																																																																																																																												            <th>
																																																																																																																												            Location
																																																																																																																												            </th>
																																																																																																																												            <th>
																																																																																																																												            Shipping Price
																																																																																																																												                </th>
																																																																																																																												                <th>

																																																																																																																												                </th>
																																																																																																																												        </tr>
																																																																																																																													    <?php
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if ($shipping_prices):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        foreach ($shipping_prices as $field) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $selected_locations_args = [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                'name'           => 'commission_shipping_location[]',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                'value'          => $field['commission_shipping_location'],
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                'select_options' => $locations,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                'disabled_label' => 'Country',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ?>
																																																																																																																																																																																																																																																										    <tr>
																																																																																																																																																																																																																																																										      <td width="45%">
																																																																																																																																																																																																				                                                                                                                                                                                                                                <?php chama_select_generator($selected_locations_args); ?>
																																																																																																																																																																																																																																																										        </td>
																																																																																																																																																																																																																																																										      <td width="45%">
																																																																																																																																																																																																																																																										      <div class="chama-input-wrapper">
																																																																																																																																																																																																																																																										      <div class="chama-input-prepend">
																																																																																																																																																																																																																																																												                                    <span class="chama-input-prepend-text">

																																																																																																																																																																																																																																																												                        <?php echo esc_html(CHAMA_SYMBOL); ?>            </span>
																																																																																																																																																																																																																																																												                                </div>
																																																																																																																																																																																																																																																												                <input class="widefat chama-input" type="number" min="0" step="any" name="commission_shipping_price[]" value="<?php echo esc_attr(empty($field['commission_shipping_price']) ? 0 : $field['commission_shipping_price']); ?>" size="30" />
																																																																																																																																																																																																																																																												                </div>
																																																																																																																																																																																																																																																										     </td>
																																																																																																																																																																																																																																																										      <td width="10%"><a class="button remove-shipping-price-row" href="#1">Remove</a></td>
																																																																																																																																																																																																																																																										    </tr>
																																																																																																																																																																																																																																																										    <?php
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        } else :
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            // show a blank one
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ?>
																																																																																																																																																																																																																																																										    <tr>
																																																																																																																																																																																																																																																										    <td width="45%">
																																																																																																																																																																																																				                                                                                                                                                                                                                            <?php chama_select_generator($unselected_locations_args); ?>
																																																																																																																																																																																																				                                                                                                                                                                                                                            </td>
																																																																																																																																																																																																																																																										        <td width="45%">
																																																																																																																																																																																																																																																										        <div class="chama-input-wrapper">
																																																																																																																																																																																																																																																										        <div class="chama-input-prepend">
																																																																																																																																																																																																																																																												                                    <span class="chama-input-prepend-text">

																																																																																																																																																																																																																																																												                        <?php echo esc_html(CHAMA_SYMBOL); ?>            </span>
																																																																																																																																																																																																																																																												                                </div>
																																																																																																																																																																																																																																																												                <input class="widefat chama-input" type="number" min="0" step="any" name="commission_shipping_price[]" value="0" size="30" />
																																																																																																																																																																																																																																																												                </div>

																																																																																																																																																																																																																																																										          </td>
																																																																																																																																																																																																																																																										          <td width="10%"><a class="button  cmb-remove-row-button button-disabled" href="#">Remove</a></td>
																																																																																																																																																																																																																																																										    </tr>
																																																																																																																																																																																																																																																										    <?php endif; ?>

																																																																																																																													    <!-- empty hidden one for jQuery -->
																																																																																																																													    <tr class="shipping-empty-row screen-reader-text">
																																																																																																																													    <td width="45%">
																																																																																																																													    <?php chama_select_generator($unselected_locations_args); ?></td>
																																																																																																																													        <td width="45%">
																																																																																																																													        <div class="chama-input-wrapper">
																																																																																																																													        <div class="chama-input-prepend">
																																																																																																																															                                    <span class="chama-input-prepend-text">

																																																																																																																															                        <?php echo esc_html(CHAMA_SYMBOL); ?>            </span>
																																																																																																																															                                </div>
																																																																																																																															                <input class="widefat chama-input" type="number" min="0" step="any" name="commission_shipping_price[]" value="0" size="30" />
																																																																																																																															                </div>
																																																																																																																													          </td>
																																																																																																																													          <td width="10%"><a class="button remove-shipping-price-row" href="#">Remove</a></td>
																																																																																																																													    </tr>
																																																																																																																													  </tbody>
																																																																																																																													</table>
																																																																																																																													<p><a id="add-shipping-price-row" class="button" href="#">Add another</a></p>
																																																																																																																													 <?php
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         endif;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //This function saves the data you put in the meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if (! function_exists('chama_commission_save_postdata')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             function chama_commission_save_postdata($post_id, $post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 if ($post->post_type == 'chama_commission') {

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     //Not save if the user hasn't submitted changes
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     // Verifying whether input is coming from the proper form
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     if (
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     ! isset($_POST['chama_commission_nonce']) ||
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     ! wp_verify_nonce(sanitize_key(wp_unslash($_POST['chama_commission_nonce'])), 'commission_metadata')
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 ) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     // Making sure the user has permission
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     if (! current_user_can('edit_post', $post_id)) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     if (! empty($_POST['commission_description'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         /* Get the posted data and sanitize it for use as an HTML class. */
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         $data = sanitize_textarea_field(wp_unslash($_POST['commission_description']));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         update_post_meta($post_id, 'commission_description', $data);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         delete_post_meta($post_id, 'commission_description');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     if (! empty($_POST['commission_price'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         /* Get the posted data and sanitize it for use as an HTML class. */
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         $data = str_replace(',', '', number_format(floatval($_POST['commission_price']), 2));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         update_post_meta($post_id, 'commission_price', $data);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         update_post_meta($post_id, 'commission_price', 0);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     if (! empty($_POST['commission_pay_what_you_want'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         /* Get the posted data and sanitize it for use as an HTML class. */
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         $data = sanitize_text_field(wp_unslash($_POST['commission_pay_what_you_want']));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if ($_POST["commission_pay_what_you_want"] == "yes") {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             $commission_pay_what_you_want = "yes";
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             $commission_pay_what_you_want = "no";
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         update_post_meta($post_id, 'commission_pay_what_you_want', $commission_pay_what_you_want);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         delete_post_meta($post_id, 'commission_pay_what_you_want');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     if (! empty($_POST['commission_add_on_name']) && ! empty($_POST['commission_add_on_price'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         $current_add_ons = get_post_meta($post_id, 'commission_add_ons', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         $new_add_ons     = [];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         $add_ons_names   = array_map('sanitize_text_field', wp_unslash($_POST['commission_add_on_name']));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         $add_ons_prices  = array_map('sanitize_text_field', wp_unslash($_POST['commission_add_on_price']));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         $count           = count($add_ons_names);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         for ($i = 0; $i < $count; $i++) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             if ($add_ons_names[$i] != ''):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 $new_add_ons[$i]['commission_add_on_name']  = sanitize_text_field(stripslashes(wp_strip_all_tags($add_ons_names[$i])));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 $new_add_ons[$i]['commission_add_on_price'] = str_replace(',', '', number_format(floatval($add_ons_prices[$i]), 2));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if (! empty($new_add_ons) && $new_add_ons != $current_add_ons) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             update_post_meta($post_id, 'commission_add_ons', $new_add_ons);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         } elseif (empty($new_add_ons) && $current_add_ons) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         delete_post_meta($post_id, 'commission_add_ons', $current_add_ons);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     update_post_meta($post_id, 'commission_add_ons', 0);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 if (! empty($_POST['commission_limit'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     /* Get the posted data and sanitize it for use as an HTML class. */
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     $data = intval($_POST['commission_limit']);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     update_post_meta($post_id, 'commission_limit', $data);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     update_post_meta($post_id, 'commission_limit', 0);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 if (! empty($_POST['commission_can_request'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     /* Get the posted data and sanitize it for use as an HTML class. */
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     $data = sanitize_text_field(wp_unslash($_POST['commission_can_request']));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     update_post_meta($post_id, 'commission_can_request', $data);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     delete_post_meta($post_id, 'commission_can_request');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 if (! empty($_POST['commission_shipping_location']) && ! empty($_POST['commission_shipping_price'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     $current_shipping_prices = get_post_meta($post_id, 'commission_shipping_prices', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     $new_shipping_prices     = [];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     $shipping_locations      = array_map('sanitize_text_field', wp_unslash($_POST['commission_shipping_location']));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     $shipping_prices         = array_map('sanitize_text_field', wp_unslash($_POST['commission_shipping_price']));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     $count                   = count($shipping_locations);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     for ($i = 0; $i < $count; $i++) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if ($shipping_locations[$i] != ''):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             $new_shipping_prices[$i]['commission_shipping_location'] = sanitize_text_field(stripslashes(wp_strip_all_tags($shipping_locations[$i])));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             $new_shipping_prices[$i]['commission_shipping_price']    = str_replace(',', '', number_format(floatval($shipping_prices[$i]), 2));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     if (! empty($new_shipping_prices) && $new_shipping_prices != $current_shipping_prices) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         update_post_meta($post_id, 'commission_shipping_prices', $new_shipping_prices);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     } elseif (empty($new_shipping_prices) && $current_shipping_prices) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         delete_post_meta($post_id, 'commission_shipping_prices', $current_shipping_prices);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     update_post_meta($post_id, 'commission_shipping_prices', 0);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 if (! empty($_POST['commission_preview_images'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     /* Get the posted data and sanitize it for use as an HTML class. */

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     $data       = sanitize_text_field(wp_unslash($_POST['commission_preview_images']));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     $data_array = array_filter(explode(',', $data));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     update_post_meta($post_id, 'commission_preview_images', $data_array);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     delete_post_meta($post_id, 'commission_preview_images');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 chama_flush_permalinks();

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //show columns
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         add_filter('manage_chama_commission_posts_columns', 'chama_commission_columns');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         function chama_commission_columns($columns)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             $columns = [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 'cb'           => $columns['cb'],
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 'title'        => __('Title', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 'price'        => __('Price', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 'flexible'     => __('Flexible Price', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 'quantity'     => __('Quantity', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 'availability' => __('Available to everyone', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 'date'         => __('Date', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             return $columns;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         add_filter('manage_chama_commission_posts_custom_column', 'chama_populate_commission_columns', 10, 2);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         function chama_populate_commission_columns($column, $id)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             switch ($column) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 case 'price':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     $price = get_post_meta($id, 'commission_price', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     echo esc_html(CHAMA_SYMBOL . number_format((float) $price, 2, '.', ''));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 case 'flexible':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     $is_flexible = get_post_meta($id, 'commission_pay_what_you_want', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     if (! empty($is_flexible)) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         echo esc_html($is_flexible);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         echo 'no';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 case 'quantity':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     $quantity = get_post_meta($id, 'commission_limit', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     if (! empty($quantity)) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         echo esc_html($quantity);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         echo 0;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 case 'availability':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     $is_flexible = get_post_meta($id, 'commission_can_request', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     if (! empty($is_flexible)) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         echo esc_html($is_flexible);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         echo 'no';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 default:
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //Extend search for commissions(description)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         add_action('admin_init', 'chama_commission_extend_search');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         function chama_commission_extend_search()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             global $typenow;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             if ($typenow === 'commission') {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 add_filter('posts_search', 'chama_commission_search_override', 10, 2);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         function chama_commission_search_override($search, $query)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             // Override the commissions page search to include custom meta data

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             global $wpdb;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             if ($query->is_main_query() && ! empty($query->query['s'])) {

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 $like   = '%' . $wpdb->esc_like($query->query['s']) . '%';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 $search = preg_replace(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "#\({$wpdb->posts}.post_title LIKE [^)]+\)\K#",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     $wpdb->prepare("
or exists (
select * from {$wpdb->postmeta} where post_id={$wpdb->posts}.ID
and meta_key in ('commission_add_ons')
and meta_value like %s
)
", $like),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     $search
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 );
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             return $search;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         /*=============================================DONATION=============================================*/
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         /**
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          * Add Donation metaboxes
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          */
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         add_action('admin_init', 'chama_donation_information_metabox');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if (! function_exists('chama_donation_information_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             function chama_donation_information_metabox()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 add_meta_box(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'donation_information',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     __('Donation Information', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'chama_donation_information_display_metabox',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     'chama_donation',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "normal",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "high"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 );

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //Displaying the meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         if (! function_exists('chama_donation_information_display_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             function chama_donation_information_display_metabox($post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 $donation_date           = get_post_meta($post->ID, 'chama_donation_date_time', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 $donation_date_formatted = gmdate("F, j, Y H:i:s", strtotime($donation_date));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 $donation_total          = get_post_meta($post->ID, 'chama_donation_total_amount', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 $donation_total_display  = CHAMA_SYMBOL . number_format((float) $donation_total, 2, '.', '');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ?>
																																																																																																																				        <h2 class="chama-metabox-heading">
																																																																																																																				        Donation Information				</h2>
																																																																																																																						<div class="chama-metabox-row">
																																																																																																																						<div class="chama-metabox-col">
																																																																																																																						<p>
																																																																																																																																		<strong>Donation Date:</strong><br>
																																																																																																																																		<?php echo esc_html($donation_date_formatted); ?>										</p>
																																																																																																																						</div>
																																																																																																																						<div class="chama-metabox-col">
																																																																																																																						<p>
																																																																																																																																		<strong>Total Donation:</strong><br>
																																																																																																																																		<?php echo esc_html($donation_total_display); ?>												</p>

																																																																																																																						</div>
																																																																																																																						</div>
																																																																																																																						<?php
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            endif;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_action('admin_init', 'chama_donor_details_metabox');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            if (! function_exists('chama_donor_details_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                function chama_donor_details_metabox()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    add_meta_box(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'donor_details',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        __('Donor Details', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'chama_donor_details_display_metabox',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'chama_donation',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "normal",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "high"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    );

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //Displaying the meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            if (! function_exists('chama_donor_details_display_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                function chama_donor_details_display_metabox($post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $user_id        = get_post_meta($post->ID, 'chama_user_id', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $user           = $user_id ? get_user_by('id', $user_id) : false;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $user_full_name = $user ? $user->first_name . ' ' . $user->last_name : '';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $user_email     = $user ? $user->user_email : '';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $donations_link = admin_url('edit.php?post_type=chama_donation');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $user_link      = admin_url('user-edit.php?user_id=') . $user_id;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    // Create account disconnect URL
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $donations_link = esc_url(add_query_arg(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'uid' => $user_id,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ],
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $donations_link
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ?>
																																																																																																																				         <h2 class="chama-metabox-heading">
																																																																																																																				         Donor Details				</h2>
																																																																																																																						<div class="chama-metabox-row">
																																																																																																																						<div class="chama-metabox-col">
																																																																																																																						<p>
																																																																																																																																		<strong>Donor ID:</strong><br>
																																																																																																																																		<?php echo esc_html($user_id); ?><br/>
																																																																																																														                                                                                <a href="<?php echo esc_url($user_link) ?>">User profile</a><br/>
																																																																																																															                                                                            <a href="<?php echo esc_url($donations_link) ?>">All donations for this donor</a>										</p>
																																																																																																																						</div>
																																																																																																																						<div class="chama-metabox-col">
																																																																																																																						<p>
																																																																																																																																		<strong>Donor Name:</strong><br>
																																																																																																																																		<?php echo esc_html($user_full_name); ?>												</p>
																																																																																																																						</div>
																																																																																																																				        <div class="chama-metabox-col">
																																																																																																																						<p>
																																																																																																																																		<strong>Donor Email:</strong><br>
																																																																																																																																		<?php echo esc_html($user_email); ?>												</p>

																																																																																																																						</div>
																																																																																																																						</div>
																																																																																																																						<?php
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //publish metabox
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_action('admin_init', 'chama_donation_publish_metabox');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            if (! function_exists('chama_donation_publish_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                function chama_donation_publish_metabox()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    add_meta_box(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'submitdiv',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        __('Update Donation', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'chama_donation_publish_display_metabox',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'chama_donation',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "side",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "default"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    );

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //Displaying the meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            if (! function_exists('chama_donation_publish_display_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                function chama_donation_publish_display_metabox($post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $delete_url         = get_delete_post_link($post->ID, '', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $status             = get_post_meta($post->ID, 'chama_payment_status', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $current_url        = home_url(chama_get_current_request_uri());
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $resend_receipt_url = esc_url(add_query_arg(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'id'           => (int) $post->ID,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'chama_nonce'  => wp_create_nonce('chama_resend_receipt_nonce'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'chama_action' => 'resend-receipt',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'type'         => 'one-time',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ],
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $current_url
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $sync_payment_url = esc_url(add_query_arg(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'id'           => (int) $post->ID,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'chama_nonce'  => wp_create_nonce('chama_sync_payment_nonce'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'chama_action' => 'sync-payment',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ],
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $current_url
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ));

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    add_query_arg('account-updated', 'true', $current_url);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ?>
																																																																																																																				        <div class="chama-publish-header">
																																																																																																																													<h3 class="hndle">Update Donation</h3>

																																																																																																																													<span class="delete-payment"><a title="Delete Donation" class="delete-single-payment delete-payment-button dashicons dashicons-trash" href="<?php echo esc_url($delete_url) ?>" aria-label="Delete Donation"></a></span>
																																																																																																																				                                </div>
																																																																																																																				                                <div class="chama-publish-box-inside ">

																																																																																																																				                                <?php
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        wp_nonce_field('publish_metadata', 'chama_publish_nonce');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            chama_price_display_metabox($post->ID, 'chama_donation_total_amount', '', 'Total Amount');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ?>
																																																																																																																				        <div>
																																																																																																																				            <label><strong>Status:</strong></label>
																																																																																																																				        </div>
																																																																																																																				         <select name="chama_payment_status" id="chama_payment_status"  >

																																																																																																																				                                        <option value="abandoned"																																																																																																																				                                                                 																																																																																																																			                                                                 																																																																																																																		                                                                 																																																																																																																	                                                                 																																																																																																																                                                                 																																																																																																															                                                                 																																																																																																														                                                                 																																																																																																													                                                                 																																																																																																												                                                                 																																																																																																											                                                                 																																																																																																										                                                                 																																																																																																									                                                                 																																																																																																								                                                                 																																																																																																							                                                                 																																																																																																						                                                                 																																																																																																					                                                                 																																																																																																				                                                                 																																																																																																			                                                                 																																																																																																		                                                                 																																																																																																	                                                                 																																																																																																                                                                 																																																																																															                                                                 																																																																																														                                                                 																																																																																													                                                                 																																																																																												                                                                 																																																																																											                                                                 																																																																																										                                                                 																																																																																									                                                                 																																																																																								                                                                 																																																																																							                                                                 																																																																																						                                                                 																																																																																					                                                                 																																																																																				                                                                 																																																																																			                                                                 																																																																																		                                                                 																																																																																	                                                                 																																																																																                                                                 																																																																															                                                                 																																																																														                                                                 																																																																													                                                                 																																																																												                                                                 																																																																											                                                                 																																																																										                                                                 																																																																									                                                                 																																																																								                                                                 																																																																							                                                                 																																																																						                                                                 																																																																					                                                                 																																																																				                                                                 																																																																			                                                                 																																																																		                                                                 																																																																	                                                                  <?php selected('abandoned', $status); ?>>Abandoned</option>
																																																																																																																				                                        <option value=" canceled"																																																																																																																				                                                                 																																																																																																																			                                                                 																																																																																																																		                                                                 																																																																																																																	                                                                 																																																																																																																                                                                 																																																																																																															                                                                 																																																																																																														                                                                 																																																																																																													                                                                 																																																																																																												                                                                 																																																																																																											                                                                 																																																																																																										                                                                 																																																																																																									                                                                 																																																																																																								                                                                 																																																																																																							                                                                 																																																																																																						                                                                 																																																																																																					                                                                 																																																																																																				                                                                 																																																																																																			                                                                 																																																																																																		                                                                 																																																																																																	                                                                 																																																																																																                                                                 																																																																																															                                                                 																																																																																														                                                                 																																																																																													                                                                 																																																																																												                                                                 																																																																																											                                                                 																																																																																										                                                                 																																																																																									                                                                 																																																																																								                                                                 																																																																																							                                                                 																																																																																						                                                                 																																																																																					                                                                 																																																																																				                                                                 																																																																																			                                                                 																																																																																		                                                                 																																																																																	                                                                 																																																																																                                                                 																																																																															                                                                 																																																																														                                                                 																																																																													                                                                 																																																																												                                                                 																																																																											                                                                 																																																																										                                                                 																																																																									                                                                 																																																																								                                                                 																																																																							                                                                 																																																																						                                                                 																																																																					                                                                 																																																																				                                                                 																																																																			                                                                 																																																																		                                                                 																																																																	                                                                  <?php selected(' canceled', $status); ?>> Canceled</option>
																																																																																																																				                                        <option value="complete"																																																																																																																				                                                                																																																																																																																			                                                                																																																																																																																		                                                                																																																																																																																	                                                                																																																																																																																                                                                																																																																																																															                                                                																																																																																																														                                                                																																																																																																													                                                                																																																																																																												                                                                																																																																																																											                                                                																																																																																																										                                                                																																																																																																									                                                                																																																																																																								                                                                																																																																																																							                                                                																																																																																																						                                                                																																																																																																					                                                                																																																																																																				                                                                																																																																																																			                                                                																																																																																																		                                                                																																																																																																	                                                                																																																																																																                                                                																																																																																															                                                                																																																																																														                                                                																																																																																													                                                                																																																																																												                                                                																																																																																											                                                                																																																																																										                                                                																																																																																									                                                                																																																																																								                                                                																																																																																							                                                                																																																																																						                                                                																																																																																					                                                                																																																																																				                                                                																																																																																			                                                                																																																																																		                                                                																																																																																	                                                                																																																																																                                                                																																																																															                                                                																																																																														                                                                																																																																													                                                                																																																																												                                                                																																																																											                                                                																																																																										                                                                																																																																									                                                                																																																																								                                                                																																																																							                                                                																																																																						                                                                																																																																					                                                                																																																																				                                                                																																																																			                                                                																																																																		                                                                																																																																	                                                                 <?php selected('complete', $status); ?>>Complete</option>
																																																																																																																				                                        <option value="failed"																																																																																																																				                                                              																																																																																																																			                                                              																																																																																																																		                                                              																																																																																																																	                                                              																																																																																																																                                                              																																																																																																															                                                              																																																																																																														                                                              																																																																																																													                                                              																																																																																																												                                                              																																																																																																											                                                              																																																																																																										                                                              																																																																																																									                                                              																																																																																																								                                                              																																																																																																							                                                              																																																																																																						                                                              																																																																																																					                                                              																																																																																																				                                                              																																																																																																			                                                              																																																																																																		                                                              																																																																																																	                                                              																																																																																																                                                              																																																																																															                                                              																																																																																														                                                              																																																																																													                                                              																																																																																												                                                              																																																																																											                                                              																																																																																										                                                              																																																																																									                                                              																																																																																								                                                              																																																																																							                                                              																																																																																						                                                              																																																																																					                                                              																																																																																				                                                              																																																																																			                                                              																																																																																		                                                              																																																																																	                                                              																																																																																                                                              																																																																															                                                              																																																																														                                                              																																																																													                                                              																																																																												                                                              																																																																											                                                              																																																																										                                                              																																																																									                                                              																																																																								                                                              																																																																							                                                              																																																																						                                                              																																																																					                                                              																																																																				                                                              																																																																			                                                              																																																																		                                                              																																																																	                                                               <?php selected('failed', $status); ?>>Failed</option>
																																																																																																																												        <option value="processing"																																																																																																																												                                  																																																																																																																											                                  																																																																																																																										                                  																																																																																																																									                                  																																																																																																																								                                  																																																																																																																							                                  																																																																																																																						                                  																																																																																																																					                                  																																																																																																																				                                  																																																																																																																			                                  																																																																																																																		                                  																																																																																																																	                                  																																																																																																																                                  																																																																																																															                                  																																																																																																														                                  																																																																																																													                                  																																																																																																												                                  																																																																																																											                                  																																																																																																										                                  																																																																																																									                                  																																																																																																								                                  																																																																																																							                                  																																																																																																						                                  																																																																																																					                                  																																																																																																				                                  																																																																																																			                                  																																																																																																		                                  																																																																																																	                                  																																																																																																                                  																																																																																															                                  																																																																																														                                  																																																																																													                                  																																																																																												                                  																																																																																											                                  																																																																																										                                  																																																																																									                                  																																																																																								                                  																																																																																							                                  																																																																																						                                  																																																																																					                                  																																																																																				                                  																																																																																			                                  																																																																																		                                  																																																																																	                                  																																																																																                                  																																																																															                                  																																																																														                                  																																																																													                                  																																																																												                                  																																																																											                                  																																																																										                                  																																																																									                                   <?php selected('processing', $status); ?>>Processing</option>
																																																																																																																				                                        <option value="refunded"																																																																																																																				                                                                																																																																																																																			                                                                																																																																																																																		                                                                																																																																																																																	                                                                																																																																																																																                                                                																																																																																																															                                                                																																																																																																														                                                                																																																																																																													                                                                																																																																																																												                                                                																																																																																																											                                                                																																																																																																										                                                                																																																																																																									                                                                																																																																																																								                                                                																																																																																																							                                                                																																																																																																						                                                                																																																																																																					                                                                																																																																																																				                                                                																																																																																																			                                                                																																																																																																		                                                                																																																																																																	                                                                																																																																																																                                                                																																																																																															                                                                																																																																																														                                                                																																																																																													                                                                																																																																																												                                                                																																																																																											                                                                																																																																																										                                                                																																																																																									                                                                																																																																																								                                                                																																																																																							                                                                																																																																																						                                                                																																																																																					                                                                																																																																																				                                                                																																																																																			                                                                																																																																																		                                                                																																																																																	                                                                																																																																																                                                                																																																																															                                                                																																																																														                                                                																																																																													                                                                																																																																												                                                                																																																																											                                                                																																																																										                                                                																																																																									                                                                																																																																								                                                                																																																																							                                                                																																																																						                                                                																																																																					                                                                																																																																				                                                                																																																																			                                                                																																																																		                                                                																																																																	                                                                 <?php selected('refunded', $status); ?>>Refunded</option>
																																																																																																																				                                        <option value="revoked"																																																																																																																				                                                               																																																																																																																			                                                               																																																																																																																		                                                               																																																																																																																	                                                               																																																																																																																                                                               																																																																																																															                                                               																																																																																																														                                                               																																																																																																													                                                               																																																																																																												                                                               																																																																																																											                                                               																																																																																																										                                                               																																																																																																									                                                               																																																																																																								                                                               																																																																																																							                                                               																																																																																																						                                                               																																																																																																					                                                               																																																																																																				                                                               																																																																																																			                                                               																																																																																																		                                                               																																																																																																	                                                               																																																																																																                                                               																																																																																															                                                               																																																																																														                                                               																																																																																													                                                               																																																																																												                                                               																																																																																											                                                               																																																																																										                                                               																																																																																									                                                               																																																																																								                                                               																																																																																							                                                               																																																																																						                                                               																																																																																					                                                               																																																																																				                                                               																																																																																			                                                               																																																																																		                                                               																																																																																	                                                               																																																																																                                                               																																																																															                                                               																																																																														                                                               																																																																													                                                               																																																																												                                                               																																																																											                                                               																																																																										                                                               																																																																									                                                               																																																																								                                                               																																																																							                                                               																																																																						                                                               																																																																					                                                               																																																																				                                                               																																																																			                                                               																																																																		                                                               																																																																	                                                                <?php selected('revoked', $status); ?>>Revoked</option>
																																																																																																																												    </select>
																																																																																																																				                                    <span class="chama-payment-status edit-status"><span class="chama-payment-status-icon																																																																																																																				                                                                                                                         																																																																																																																			                                                                                                                         																																																																																																																		                                                                                                                         																																																																																																																	                                                                                                                         																																																																																																																                                                                                                                         																																																																																																															                                                                                                                         																																																																																																														                                                                                                                         																																																																																																													                                                                                                                         																																																																																																												                                                                                                                         																																																																																																											                                                                                                                         																																																																																																										                                                                                                                         																																																																																																									                                                                                                                         																																																																																																								                                                                                                                         																																																																																																							                                                                                                                         																																																																																																						                                                                                                                         																																																																																																					                                                                                                                         																																																																																																				                                                                                                                         																																																																																																			                                                                                                                         																																																																																																		                                                                                                                         																																																																																																	                                                                                                                         																																																																																																                                                                                                                         																																																																																															                                                                                                                         																																																																																														                                                                                                                         																																																																																													                                                                                                                         																																																																																												                                                                                                                         																																																																																											                                                                                                                         																																																																																										                                                                                                                         																																																																																									                                                                                                                         																																																																																								                                                                                                                         																																																																																							                                                                                                                         																																																																																						                                                                                                                         																																																																																					                                                                                                                         																																																																																				                                                                                                                         																																																																																			                                                                                                                         																																																																																		                                                                                                                         																																																																																	                                                                                                                         																																																																																                                                                                                                         																																																																															                                                                                                                         																																																																														                                                                                                                         																																																																													                                                                                                                         																																																																												                                                                                                                         																																																																											                                                                                                                         																																																																										                                                                                                                         																																																																									                                                                                                                         																																																																								                                                                                                                         																																																																							                                                                                                                         																																																																						                                                                                                                         																																																																					                                                                                                                         																																																																				                                                                                                                         																																																																			                                                                                                                         																																																																		                                                                                                                         																																																																	                                                                                                                          <?php echo esc_attr($status); ?>"></span></span>
																																																																																																																				                                    <br/><br/>
																																																																																																																				                                </div>

																																																																																																																				<div class="chama-admin-box">

																																																																																																																													<div id="major-publishing-actions">
																																																																																																																														<div id="publishing-action">
																																																																																																																															<input type="submit" class="button button-primary right" value="Save Donation">
																																																																																																														                                                                    <?php if ($status === 'complete'): ?>
																																																																																																																															<a href="<?php echo esc_url($resend_receipt_url); ?>"  class="button-secondary right chama-resend-receipt">Resend Receipt</a>
																																																																																																											                                                                                <?php else: ?>
                                                                                    <a href="<?php echo esc_url($sync_payment_url); ?>"  class="button-secondary right chama-sync-payment">Sync Donation</a>
			                                                                    <?php endif; ?>									</div>
																<div class="clear"></div>
															</div>

														</div>
								<?php
                                    }
                                    endif;
                                    //stripe metabox
                                    add_action('admin_init', 'chama_donation_stripe_meta_metabox');
                                    if (! function_exists('chama_donation_stripe_meta_metabox')):
                                        function chama_donation_stripe_meta_metabox()
                                    {
                                            add_meta_box(
                                                'stripe_meta',
                                                __('Stripe Meta', 'chama'),
                                                'chama_donation_stripe_meta_display_metabox',
                                                'chama_donation',
                                                "side",
                                                "default"
                                            );

                                        }
                                    endif;
                                    //Displaying the meta box
                                    if (! function_exists('chama_donation_stripe_meta_display_metabox')):
                                        function chama_donation_stripe_meta_display_metabox($post)
                                    {
                                            $stripe_customer_id       = get_post_meta($post->ID, 'chama_stripe_customer_id', true);
                                            $stripe_payment_intent_id = get_post_meta($post->ID, 'chama_stripe_payment_intent_id', true);
                                        ?>
																																																																																																																				<p>
																																																																																																																				<strong>Transaction ID:</strong><br>
																																																																																																																				<?php echo esc_html($stripe_payment_intent_id); ?>
																																																																																																																				</p>
																																																																																																																				<p>
																																																																																																																				<strong>Customer ID:</strong><br>
																																																																																																																				<?php echo esc_html($stripe_customer_id); ?>
																																																																																																																				</p>
																																																																																																																						<?php
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            endif;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //This function saves the data you put in the meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            if (! function_exists('chama_donation_save_postdata')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                function chama_donation_save_postdata($post_id, $post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    if ((isset($_POST['chama_publish_nonce'])) && $post->post_type == 'chama_donation') {

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        //Not save if the user hasn't submitted changes
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        // Verifying whether input is coming from the proper form
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if (! wp_verify_nonce(sanitize_key($_POST['chama_publish_nonce']), 'publish_metadata')) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        // Making sure the user has permission
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if (! current_user_can('edit_post', $post_id)) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if (! empty($_POST['chama_donation_total_amount'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            /* Get the posted data and sanitize it for use as an HTML class. */
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $data = str_replace(',', '', number_format(floatval($_POST['chama_donation_total_amount']), 2));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            update_post_meta($post_id, 'chama_donation_total_amount', $data);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            update_post_meta($post_id, 'chama_donation_total_amount', 0);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if (! empty($_POST['chama_payment_status'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            /* Get the posted data and sanitize it for use as an HTML class. */
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $data = sanitize_text_field(wp_unslash($_POST['chama_payment_status']));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            update_post_meta($post_id, 'chama_payment_status', $data);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            update_post_meta($post_id, 'chama_payment_status', 'processing');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_action('save_post_chama_donation', 'chama_donation_save_postdata', 20, 2);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //show columns
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_filter('manage_chama_donation_posts_columns', 'chama_donation_columns');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_donation_columns($columns)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                unset($columns['date']);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns = [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'cb'           => $columns['cb'],
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'donor'        => __('Donor', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'status'       => __('Status', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'payment_date' => __('Date', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'amount'       => __('Amount', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'details'      => __('Details', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                return $columns;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_filter('manage_chama_donation_posts_custom_column', 'chama_populate_donation_columns', 10, 2);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_populate_donation_columns($column, $id)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                switch ($column) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    case 'amount':

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $currency = get_post_meta($id, 'chama_donation_currency', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $amount   = get_post_meta($id, 'chama_donation_total_amount', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        echo esc_html(CHAMA_SYMBOL . number_format((float) $amount, 2, '.', ''));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    case 'payment_date':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $donation_date           = get_post_meta($id, 'chama_donation_date_time', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $donation_date_formatted = gmdate("F, j, Y", strtotime($donation_date));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        echo esc_html($donation_date_formatted);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    case 'details':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $edit_link = get_edit_post_link($id);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        echo '<a href="' . esc_url($edit_link) . '" class="button button-small hint--top hint--bounce"><span class="dashicons dashicons-visibility"></span></a>';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    case 'status':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $test_mode        = get_post_meta($id, 'chama_is_test_donation', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $test_mode_notice = $test_mode ? '<span  class="chama-test-mode-label label-listing" aria-label="This donation was made in test mode." rel="tooltip">Test</span>' : '';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $status           = get_post_meta($id, 'chama_payment_status', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        echo '<span class="chama-payment-status listing-status"><span class="chama-payment-status-icon ' . esc_attr($status) . '"></span>&nbsp;' . esc_html(ucwords($status)) . '</span> ' . wp_kses($test_mode_notice, chama_allowed_html());
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    case 'donor':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $user_id        = get_post_meta($id, 'chama_user_id', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $user           = $user_id ? get_user_by('id', $user_id) : false;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $user_full_name = $user && $user->first_name && $user->last_name ? $user->first_name . ' ' . $user->last_name : false;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $user_email     = $user ? $user->user_email : '';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $display_name   = $user_full_name ? $user_full_name : $user_email;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $user_link      = admin_url('user-edit.php?user_id=') . $user_id;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        echo '<a href="' . esc_url($user_link) . '">' . esc_html($user_id) . ' - ' . esc_html($display_name) . '</a>';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    default:
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //Extend search for donations(description)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_action('admin_init', 'chama_donation_extend_search');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_donation_extend_search()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                global $typenow;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if ($typenow === 'chama_donation') {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    add_filter('posts_search', 'chama_donation_search_override', 10, 2);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_donation_search_override($search, $query)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                global $wpdb;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if ($query->is_main_query() && ! empty($query->query['s'])) {

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $like   = '%' . $wpdb->esc_like($query->query['s']) . '%';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $search = preg_replace("#\({$wpdb->posts}.post_title LIKE [^)]+\)\K#",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $wpdb->prepare("
or exists (
select * from {$wpdb->postmeta} where post_id={$wpdb->posts}.ID
and meta_key in ('chama_user_id', 'chama_email', 'chama_full_name')
and meta_value like %s
)
", $like), $search);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                return $search;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //Filter donations
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_action('parse_query', 'chama_filter_donations');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_filter_donations($query)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                global $pagenow;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if (! is_admin() || 'edit.php' !== $pagenow || ! isset($query->query['post_type']) || 'chama_donation' !== $query->query['post_type']) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $nonce = '';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if (isset($_GET['cfn'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $nonce = sanitize_text_field(wp_unslash($_GET['cfn']));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $user_id = isset($_GET['uid']) ? sanitize_text_field(wp_unslash($_GET['uid'])) : '';

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if (empty($nonce) || ! wp_verify_nonce($nonce, 'chama_filter_nonce') || empty($user_id)) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if ($query->is_main_query()) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $query->set('meta_query', [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'key'   => 'chama_user_id',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'value' => $user_id,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ],
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ]);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            /*=============================================SUBSCRIPTION=============================================*/
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            /**
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             * Add Subscription metaboxes
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             */
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_action('admin_init', 'chama_subscription_information_metabox');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            if (! function_exists('chama_subscription_information_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                function chama_subscription_information_metabox()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    add_meta_box(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'subscription_information',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        __('Subscription Information', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'chama_subscription_information_display_metabox',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'chama_subscription',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "normal",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "high"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    );

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //Displaying the meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            if (! function_exists('chama_subscription_information_display_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                function chama_subscription_information_display_metabox($post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $subscription_date           = get_post_meta($post->ID, 'chama_subscription_date_time', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $subscription_date_formatted = gmdate("F, j, Y H:i:s", strtotime($subscription_date));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $subscription_renewal_date   = get_post_meta($post->ID, 'chama_subscription_renewal_date', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $subscription_total          = get_post_meta($post->ID, 'chama_subscription_total_amount', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $subscription_total_display  = CHAMA_SYMBOL . number_format((float) $subscription_total, 2, '.', '');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $subscription_tier           = get_post_meta($post->ID, 'chama_subscription_description', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $subscription_event          = get_post_meta($post->ID, 'chama_subscription_event', true);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ?>
																																																																																																																				        <h2 class="chama-metabox-heading">
																																																																																																																				        Subscription Information</h2>
																																																																																																															<div class="chama-metabox-row">
																																																																																																															<div class="chama-metabox-col">
																																																																																																															<p>
																																																																																																																											<strong>Transaction Date:</strong><br>
																																																																																																																											<?php echo esc_html($subscription_date_formatted); ?>										</p>
																																																																																																															</div>
																																																																																							                                                                                                <div class="chama-metabox-col">
																																																																																																															<p>
																																																																																																																											<strong>Renewal Date:</strong><br>
																																																																																																																											<?php echo esc_html($subscription_renewal_date); ?>										</p>
																																																																																																															</div>
																																																																																							                                                                                                <div class="chama-metabox-col">
																																																																																																															<p>
																																																																																																																											<strong>Transaction Event:</strong><br>
																																																																																																																											<?php echo 'Subscription ' . esc_html($subscription_event); ?> <span class="chama-subscription-status-icon<?php echo esc_attr($subscription_event); ?>"></span>												</p>
																																																																																																															</div>
																																																																																																															<div class="chama-metabox-col">
																																																																																																															<p>
																																																																																																																											<strong>Total Amount:</strong><br>
																																																																																																																											<?php echo esc_html($subscription_total_display); ?>												</p>
																																																																																																															</div>
																																																																																																				                                            <div class="chama-metabox-col">
																																																																																																															<p>
																																																																																																																											<strong>Tier:</strong><br>
																																																																																																																											<?php echo esc_html($subscription_tier); ?>												</p>
																																																																																																															</div>
																																																																																																															</div>

																																																																																																															<?php
                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                endif;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                add_action('admin_init', 'chama_member_details_metabox');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                if (! function_exists('chama_member_details_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                    function chama_member_details_metabox()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                        add_meta_box(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'member_details',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                            __('Member Details', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'chama_member_details_display_metabox',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'chama_subscription',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                            "normal",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                            "high"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                        );

                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                //Displaying the meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                if (! function_exists('chama_member_details_display_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                    function chama_member_details_display_metabox($post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                {

                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $user_id                   = get_post_meta($post->ID, 'chama_user_id', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $user                      = $user_id ? get_user_by('id', $user_id) : false;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $user_full_name            = $user ? $user->first_name . ' ' . $user->last_name : '';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $user_email                = $user ? $user->user_email : '';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $transactions_link         = $subscriptions_link         = admin_url('edit.php?post_type=chama_subscription');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $user_link                 = admin_url('user-edit.php?user_id=') . $user_id;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $payment_method_id         = get_user_meta($user_id, 'chama_payment_method_id', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $payment_method_brand      = get_user_meta($user_id, 'chama_payment_method_brand', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $payment_method_expiration = get_user_meta($user_id, 'chama_payment_method_expiration', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $payment_method_last4      = get_user_meta($user_id, 'chama_payment_method_last4', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $stripe_subscription_id    = get_post_meta($post->ID, 'chama_stripe_subscription_id', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $chama_filter_nonce        = wp_create_nonce('chama_filter_nonce');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                        // Create account disconnect URL
                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $subscriptions_link = esc_url(add_query_arg(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                            [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                'uid' => $user_id,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                'cfn' => $chama_filter_nonce,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ],
                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $subscriptions_link
                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $transactions_link = esc_url(add_query_arg(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                            [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                'sid' => $stripe_subscription_id,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                'cfn' => $chama_filter_nonce,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ],
                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $transactions_link
                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ?>
																																																																																																																				         <h2 class="chama-metabox-heading">
																																																																																																																				         Member Details				</h2>
																																																																																																																						<div class="chama-metabox-row">
																																																																																																																						<div class="chama-metabox-col">
																																																																																																																						<p>
																																																																																																																																		<strong>Member ID:</strong><br>
																																																																																																																																		<?php echo esc_html($user_id); ?><br/>
																																																																																																														                                                                                <a href="<?php echo esc_url($user_link) ?>">User profile</a><br/>
																																																																																                                                                                                                                                                                                        <a href="<?php echo esc_url($transactions_link) ?>">All related transactions</a><br/>
																																																																																																															                                                                            <a href="<?php echo esc_url($subscriptions_link) ?>">All subscriptions for this member</a><br/>
																																																																																                                                                                                                                                                                                    										</p>
																																																																																																																						</div>
																																																																																																																						<div class="chama-metabox-col">
																																																																																																																						<p>
																																																																																																																																		<strong>Member Name:</strong><br>
																																																																																																																																		<?php echo esc_html($user_full_name); ?>												</p>
																																																																																																																						</div>
																																																																																																																				        <div class="chama-metabox-col">
																																																																																																																						<p>
																																																																																																																																		<strong>Member Email:</strong><br>
																																																																																																																																		<?php echo esc_html($user_email); ?>												</p>
																																																																																																																						</div>
																																																																																							                                                                                                                            <div class="chama-metabox-col">
																																																																																																																						<p>
																																																																																																																																		<strong>Payment Card:</strong><br>
																																																																																																																																		<?php echo esc_html($payment_method_brand); ?><br/>
																																																																																							                                                                                                                                                                            <?php echo esc_html($payment_method_expiration); ?><br/>
																																																																																							                                                                                                                                                                            <?php echo esc_html($payment_method_last4); ?><br/>												</p>
																																																																																																																						</div>
																																																																																																																						</div>

																																																																																																																						<?php
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //publish metabox
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_action('admin_init', 'chama_subscription_publish_metabox');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            if (! function_exists('chama_subscription_publish_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                function chama_subscription_publish_metabox()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    add_meta_box(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'submitdiv',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        __('Update Subscription', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'chama_subscription_publish_display_metabox',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'chama_subscription',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "side",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "default"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    );

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //Displaying the meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            if (! function_exists('chama_subscription_publish_display_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                function chama_subscription_publish_display_metabox($post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $delete_url          = get_delete_post_link($post->ID, '', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $payment_status      = get_post_meta($post->ID, 'chama_payment_status', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $subscription_status = get_post_meta($post->ID, 'chama_subscription_status', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $current_url         = home_url(chama_get_current_request_uri());
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $resend_receipt_url  = esc_url(add_query_arg(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'id'           => (int) $post->ID,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'chama_nonce'  => wp_create_nonce('chama_resend_receipt_nonce'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'chama_action' => 'resend-receipt',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'type'         => 'membership',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ],
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $current_url
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $sync_payment_url = esc_url(add_query_arg(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'id'           => (int) $post->ID,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'chama_nonce'  => wp_create_nonce('chama_sync_payment_nonce'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'chama_action' => 'sync-payment',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ],
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $current_url
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $sync_subscription_url = esc_url(add_query_arg(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'id'           => (int) $post->ID,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'chama_nonce'  => wp_create_nonce('chama_sync_subscription_nonce'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'chama_action' => 'sync-subscription',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ],
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $current_url
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    add_query_arg('account-updated', 'true', $current_url);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ?>
																																																																																																																				        <div class="chama-publish-header">
																																																																																																																													<h3 class="hndle">Update Subscription</h3>

																																																																																																																													<span class="delete-payment"><a title="Delete Subscription" class="delete-single-payment delete-payment-button dashicons dashicons-trash" href="<?php echo esc_url($delete_url) ?>" aria-label="Delete Subscription"></a></span>
																																																																																																																				                                </div>
																																																																																																																				                                <div class="chama-publish-box-inside ">

																																																																																																																				                                <?php
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        wp_nonce_field('publish_metadata', 'chama_publish_nonce');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            chama_price_display_metabox($post->ID, 'chama_subscription_total_amount', '', 'Total Amount');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ?>
																																																																																																																				        <div>
																																																																																																																				            <label><strong>Payment Status:</strong></label>
																																																																																																																				        </div>
																																																																																																																				         <select name="chama_payment_status" id="chama_payment_status"  >

																																																																																																																				                                        <option value="abandoned"																																																																																																																				                                                                 																																																																																																																			                                                                 																																																																																																																		                                                                 																																																																																																																	                                                                 																																																																																																																                                                                 																																																																																																															                                                                 																																																																																																														                                                                 																																																																																																													                                                                 																																																																																																												                                                                 																																																																																																											                                                                 																																																																																																										                                                                 																																																																																																									                                                                 																																																																																																								                                                                 																																																																																																							                                                                 																																																																																																						                                                                 																																																																																																					                                                                 																																																																																																				                                                                 																																																																																																			                                                                 																																																																																																		                                                                 																																																																																																	                                                                 																																																																																																                                                                 																																																																																															                                                                 																																																																																														                                                                 																																																																																													                                                                 																																																																																												                                                                 																																																																																											                                                                 																																																																																										                                                                 																																																																																									                                                                 																																																																																								                                                                 																																																																																							                                                                 																																																																																						                                                                 																																																																																					                                                                 																																																																																				                                                                 																																																																																			                                                                 																																																																																		                                                                 																																																																																	                                                                 																																																																																                                                                 																																																																															                                                                 																																																																														                                                                 																																																																													                                                                 																																																																												                                                                 																																																																											                                                                 																																																																										                                                                 																																																																									                                                                 																																																																								                                                                 																																																																							                                                                 																																																																						                                                                 																																																																					                                                                 																																																																				                                                                 																																																																			                                                                 																																																																		                                                                 																																																																	                                                                  <?php selected('abandoned', $payment_status); ?>>Abandoned</option>
																																																																																																																				                                        <option value="canceled"																																																																																																																				                                                                																																																																																																																			                                                                																																																																																																																		                                                                																																																																																																																	                                                                																																																																																																																                                                                																																																																																																															                                                                																																																																																																														                                                                																																																																																																													                                                                																																																																																																												                                                                																																																																																																											                                                                																																																																																																										                                                                																																																																																																									                                                                																																																																																																								                                                                																																																																																																							                                                                																																																																																																						                                                                																																																																																																					                                                                																																																																																																				                                                                																																																																																																			                                                                																																																																																																		                                                                																																																																																																	                                                                																																																																																																                                                                																																																																																															                                                                																																																																																														                                                                																																																																																													                                                                																																																																																												                                                                																																																																																											                                                                																																																																																										                                                                																																																																																									                                                                																																																																																								                                                                																																																																																							                                                                																																																																																						                                                                																																																																																					                                                                																																																																																				                                                                																																																																																			                                                                																																																																																		                                                                																																																																																	                                                                																																																																																                                                                																																																																															                                                                																																																																														                                                                																																																																													                                                                																																																																												                                                                																																																																											                                                                																																																																										                                                                																																																																									                                                                																																																																								                                                                																																																																							                                                                																																																																						                                                                																																																																					                                                                																																																																				                                                                																																																																			                                                                																																																																		                                                                																																																																	                                                                 <?php selected('canceled', $payment_status); ?>>Canceled</option>
																																																																																																																				                                        <option value="complete"																																																																																																																				                                                                																																																																																																																			                                                                																																																																																																																		                                                                																																																																																																																	                                                                																																																																																																																                                                                																																																																																																															                                                                																																																																																																														                                                                																																																																																																													                                                                																																																																																																												                                                                																																																																																																											                                                                																																																																																																										                                                                																																																																																																									                                                                																																																																																																								                                                                																																																																																																							                                                                																																																																																																						                                                                																																																																																																					                                                                																																																																																																				                                                                																																																																																																			                                                                																																																																																																		                                                                																																																																																																	                                                                																																																																																																                                                                																																																																																															                                                                																																																																																														                                                                																																																																																													                                                                																																																																																												                                                                																																																																																											                                                                																																																																																										                                                                																																																																																									                                                                																																																																																								                                                                																																																																																							                                                                																																																																																						                                                                																																																																																					                                                                																																																																																				                                                                																																																																																			                                                                																																																																																		                                                                																																																																																	                                                                																																																																																                                                                																																																																															                                                                																																																																														                                                                																																																																													                                                                																																																																												                                                                																																																																											                                                                																																																																										                                                                																																																																									                                                                																																																																								                                                                																																																																							                                                                																																																																						                                                                																																																																					                                                                																																																																				                                                                																																																																			                                                                																																																																		                                                                																																																																	                                                                 <?php selected('complete', $payment_status); ?>>Complete</option>
																																																																																																																				                                        <option value="failed"																																																																																																																				                                                              																																																																																																																			                                                              																																																																																																																		                                                              																																																																																																																	                                                              																																																																																																																                                                              																																																																																																															                                                              																																																																																																														                                                              																																																																																																													                                                              																																																																																																												                                                              																																																																																																											                                                              																																																																																																										                                                              																																																																																																									                                                              																																																																																																								                                                              																																																																																																							                                                              																																																																																																						                                                              																																																																																																					                                                              																																																																																																				                                                              																																																																																																			                                                              																																																																																																		                                                              																																																																																																	                                                              																																																																																																                                                              																																																																																															                                                              																																																																																														                                                              																																																																																													                                                              																																																																																												                                                              																																																																																											                                                              																																																																																										                                                              																																																																																									                                                              																																																																																								                                                              																																																																																							                                                              																																																																																						                                                              																																																																																					                                                              																																																																																				                                                              																																																																																			                                                              																																																																																		                                                              																																																																																	                                                              																																																																																                                                              																																																																															                                                              																																																																														                                                              																																																																													                                                              																																																																												                                                              																																																																											                                                              																																																																										                                                              																																																																									                                                              																																																																								                                                              																																																																							                                                              																																																																						                                                              																																																																					                                                              																																																																				                                                              																																																																			                                                              																																																																		                                                              																																																																	                                                               <?php selected('failed', $payment_status); ?>>Failed</option>
																																																																																																																												        <option value="processing"																																																																																																																												                                  																																																																																																																											                                  																																																																																																																										                                  																																																																																																																									                                  																																																																																																																								                                  																																																																																																																							                                  																																																																																																																						                                  																																																																																																																					                                  																																																																																																																				                                  																																																																																																																			                                  																																																																																																																		                                  																																																																																																																	                                  																																																																																																																                                  																																																																																																															                                  																																																																																																														                                  																																																																																																													                                  																																																																																																												                                  																																																																																																											                                  																																																																																																										                                  																																																																																																									                                  																																																																																																								                                  																																																																																																							                                  																																																																																																						                                  																																																																																																					                                  																																																																																																				                                  																																																																																																			                                  																																																																																																		                                  																																																																																																	                                  																																																																																																                                  																																																																																															                                  																																																																																														                                  																																																																																													                                  																																																																																												                                  																																																																																											                                  																																																																																										                                  																																																																																									                                  																																																																																								                                  																																																																																							                                  																																																																																						                                  																																																																																					                                  																																																																																				                                  																																																																																			                                  																																																																																		                                  																																																																																	                                  																																																																																                                  																																																																															                                  																																																																														                                  																																																																													                                  																																																																												                                  																																																																											                                  																																																																										                                  																																																																									                                   <?php selected('processing', $payment_status); ?>>Processing</option>
																																																																																																																				                                        <option value="refunded"																																																																																																																				                                                                																																																																																																																			                                                                																																																																																																																		                                                                																																																																																																																	                                                                																																																																																																																                                                                																																																																																																															                                                                																																																																																																														                                                                																																																																																																													                                                                																																																																																																												                                                                																																																																																																											                                                                																																																																																																										                                                                																																																																																																									                                                                																																																																																																								                                                                																																																																																																							                                                                																																																																																																						                                                                																																																																																																					                                                                																																																																																																				                                                                																																																																																																			                                                                																																																																																																		                                                                																																																																																																	                                                                																																																																																																                                                                																																																																																															                                                                																																																																																														                                                                																																																																																													                                                                																																																																																												                                                                																																																																																											                                                                																																																																																										                                                                																																																																																									                                                                																																																																																								                                                                																																																																																							                                                                																																																																																						                                                                																																																																																					                                                                																																																																																				                                                                																																																																																			                                                                																																																																																		                                                                																																																																																	                                                                																																																																																                                                                																																																																															                                                                																																																																														                                                                																																																																													                                                                																																																																												                                                                																																																																											                                                                																																																																										                                                                																																																																									                                                                																																																																								                                                                																																																																							                                                                																																																																						                                                                																																																																					                                                                																																																																				                                                                																																																																			                                                                																																																																		                                                                																																																																	                                                                 <?php selected('refunded', $payment_status); ?>>Refunded</option>
																																																																																																																				                                        <option value="revoked"																																																																																																																				                                                               																																																																																																																			                                                               																																																																																																																		                                                               																																																																																																																	                                                               																																																																																																																                                                               																																																																																																															                                                               																																																																																																														                                                               																																																																																																													                                                               																																																																																																												                                                               																																																																																																											                                                               																																																																																																										                                                               																																																																																																									                                                               																																																																																																								                                                               																																																																																																							                                                               																																																																																																						                                                               																																																																																																					                                                               																																																																																																				                                                               																																																																																																			                                                               																																																																																																		                                                               																																																																																																	                                                               																																																																																																                                                               																																																																																															                                                               																																																																																														                                                               																																																																																													                                                               																																																																																												                                                               																																																																																											                                                               																																																																																										                                                               																																																																																									                                                               																																																																																								                                                               																																																																																							                                                               																																																																																						                                                               																																																																																					                                                               																																																																																				                                                               																																																																																			                                                               																																																																																		                                                               																																																																																	                                                               																																																																																                                                               																																																																															                                                               																																																																														                                                               																																																																													                                                               																																																																												                                                               																																																																											                                                               																																																																										                                                               																																																																									                                                               																																																																								                                                               																																																																							                                                               																																																																						                                                               																																																																					                                                               																																																																				                                                               																																																																			                                                               																																																																		                                                               																																																																	                                                                <?php selected('revoked', $payment_status); ?>>Revoked</option>
																																																																																																																												    </select>
																																																																																																																				                                    <span class="chama-payment-status edit-status"><span class="chama-payment-status-icon																																																																																																																				                                                                                                                         																																																																																																																			                                                                                                                         																																																																																																																		                                                                                                                         																																																																																																																	                                                                                                                         																																																																																																																                                                                                                                         																																																																																																															                                                                                                                         																																																																																																														                                                                                                                         																																																																																																													                                                                                                                         																																																																																																												                                                                                                                         																																																																																																											                                                                                                                         																																																																																																										                                                                                                                         																																																																																																									                                                                                                                         																																																																																																								                                                                                                                         																																																																																																							                                                                                                                         																																																																																																						                                                                                                                         																																																																																																					                                                                                                                         																																																																																																				                                                                                                                         																																																																																																			                                                                                                                         																																																																																																		                                                                                                                         																																																																																																	                                                                                                                         																																																																																																                                                                                                                         																																																																																															                                                                                                                         																																																																																														                                                                                                                         																																																																																													                                                                                                                         																																																																																												                                                                                                                         																																																																																											                                                                                                                         																																																																																										                                                                                                                         																																																																																									                                                                                                                         																																																																																								                                                                                                                         																																																																																							                                                                                                                         																																																																																						                                                                                                                         																																																																																					                                                                                                                         																																																																																				                                                                                                                         																																																																																			                                                                                                                         																																																																																		                                                                                                                         																																																																																	                                                                                                                         																																																																																                                                                                                                         																																																																															                                                                                                                         																																																																														                                                                                                                         																																																																													                                                                                                                         																																																																												                                                                                                                         																																																																											                                                                                                                         																																																																										                                                                                                                         																																																																									                                                                                                                         																																																																								                                                                                                                         																																																																							                                                                                                                         																																																																						                                                                                                                         																																																																					                                                                                                                         																																																																				                                                                                                                         																																																																			                                                                                                                         																																																																		                                                                                                                         																																																																	                                                                                                                          <?php echo esc_attr($payment_status); ?>"></span></span>
																																																																																																																				                                    <br/>
																																																																																												                                                                                                                                    <br/>
																																																																																												                                                                                                                                    <div>
																																																																																																																				            <label><strong>Subscription Status:</strong></label>
																																																																																																																				        </div>
																																																																																												                                                                                                        <select name="chama_subscription_status" id="chama_subscription_status"  >

																																																																																												<option value="active"																																																																																												                      																																																																																											                      																																																																																										                      																																																																																									                      																																																																																								                      																																																																																							                      																																																																																						                      																																																																																					                      																																																																																				                      																																																																																			                      																																																																																		                      																																																																																	                      																																																																																                      																																																																															                      																																																																														                      																																																																													                      																																																																												                      																																																																											                      																																																																										                      																																																																									                      																																																																								                      																																																																							                      																																																																						                      																																																																					                      																																																																				                      																																																																			                      																																																																		                      																																																																	                      																																																																                      																																																															                      																																																														                      																																																													                      																																																												                      																																																											                      																																																										                      																																																									                      																																																								                      																																																							                      																																																						                      																																																					                      																																																				                      																																																			                      																																																		                      																																																	                      																																																                      																																															                      																																														                      																																													                      																																												                      																																											                      																																										                      																																									                       <?php selected('active', $subscription_status); ?>>Active</option>
																																																																																												<option value="canceled"																																																																																												                        																																																																																											                        																																																																																										                        																																																																																									                        																																																																																								                        																																																																																							                        																																																																																						                        																																																																																					                        																																																																																				                        																																																																																			                        																																																																																		                        																																																																																	                        																																																																																                        																																																																															                        																																																																														                        																																																																													                        																																																																												                        																																																																											                        																																																																										                        																																																																									                        																																																																								                        																																																																							                        																																																																						                        																																																																					                        																																																																				                        																																																																			                        																																																																		                        																																																																	                        																																																																                        																																																															                        																																																														                        																																																													                        																																																												                        																																																											                        																																																										                        																																																									                        																																																								                        																																																							                        																																																						                        																																																					                        																																																				                        																																																			                        																																																		                        																																																	                        																																																                        																																															                        																																														                        																																													                        																																												                        																																											                        																																										                        																																									                         <?php selected('canceled', $subscription_status); ?>>Canceled</option>
																																																																																							                    <option value="inactive"																																																																																							                                            																																																																																						                                            																																																																																					                                            																																																																																				                                            																																																																																			                                            																																																																																		                                            																																																																																	                                            																																																																																                                            																																																																															                                            																																																																														                                            																																																																													                                            																																																																												                                            																																																																											                                            																																																																										                                            																																																																									                                            																																																																								                                            																																																																							                                            																																																																						                                            																																																																					                                            																																																																				                                            																																																																			                                            																																																																		                                            																																																																	                                            																																																																                                            																																																															                                            																																																														                                            																																																													                                            																																																												                                            																																																											                                            																																																										                                            																																																									                                            																																																								                                            																																																							                                            																																																						                                            																																																					                                            																																																				                                            																																																			                                            																																																		                                            																																																	                                            																																																                                            																																															                                            																																														                                            																																													                                            																																												                                            																																											                                            																																										                                            																																									                                            																																								                                            																																							                                            																																						                                            																																					                                            																																				                                             <?php selected('inactive', $subscription_status); ?>>Inactive</option>
																																																																																												<option value="incomplete"																																																																																												                          																																																																																											                          																																																																																										                          																																																																																									                          																																																																																								                          																																																																																							                          																																																																																						                          																																																																																					                          																																																																																				                          																																																																																			                          																																																																																		                          																																																																																	                          																																																																																                          																																																																															                          																																																																														                          																																																																													                          																																																																												                          																																																																											                          																																																																										                          																																																																									                          																																																																								                          																																																																							                          																																																																						                          																																																																					                          																																																																				                          																																																																			                          																																																																		                          																																																																	                          																																																																                          																																																															                          																																																														                          																																																													                          																																																												                          																																																											                          																																																										                          																																																									                          																																																								                          																																																							                          																																																						                          																																																					                          																																																				                          																																																			                          																																																		                          																																																	                          																																																                          																																															                          																																														                          																																													                          																																												                          																																											                          																																										                          																																									                           <?php selected('incomplete', $subscription_status); ?>>Incomplete</option>
																																																																																												<option value="incomplete_expired"																																																																																												                                  																																																																																											                                  																																																																																										                                  																																																																																									                                  																																																																																								                                  																																																																																							                                  																																																																																						                                  																																																																																					                                  																																																																																				                                  																																																																																			                                  																																																																																		                                  																																																																																	                                  																																																																																                                  																																																																															                                  																																																																														                                  																																																																													                                  																																																																												                                  																																																																											                                  																																																																										                                  																																																																									                                  																																																																								                                  																																																																							                                  																																																																						                                  																																																																					                                  																																																																				                                  																																																																			                                  																																																																		                                  																																																																	                                  																																																																                                  																																																															                                  																																																														                                  																																																													                                  																																																												                                  																																																											                                  																																																										                                  																																																									                                  																																																								                                  																																																							                                  																																																						                                  																																																					                                  																																																				                                  																																																			                                  																																																		                                  																																																	                                  																																																                                  																																															                                  																																														                                  																																													                                  																																												                                  																																											                                  																																										                                  																																									                                   <?php selected('incomplete_expired', $subscription_status); ?>>Incomplete Expired</option>
																																																																																												<option value="past_due"																																																																																												                        																																																																																											                        																																																																																										                        																																																																																									                        																																																																																								                        																																																																																							                        																																																																																						                        																																																																																					                        																																																																																				                        																																																																																			                        																																																																																		                        																																																																																	                        																																																																																                        																																																																															                        																																																																														                        																																																																													                        																																																																												                        																																																																											                        																																																																										                        																																																																									                        																																																																								                        																																																																							                        																																																																						                        																																																																					                        																																																																				                        																																																																			                        																																																																		                        																																																																	                        																																																																                        																																																															                        																																																														                        																																																													                        																																																												                        																																																											                        																																																										                        																																																									                        																																																								                        																																																							                        																																																						                        																																																					                        																																																				                        																																																			                        																																																		                        																																																	                        																																																                        																																															                        																																														                        																																													                        																																												                        																																											                        																																										                        																																									                         <?php selected('past_due', $subscription_status); ?>>Past Due</option>
																																																																																												<option value="paused"																																																																																												                      																																																																																											                      																																																																																										                      																																																																																									                      																																																																																								                      																																																																																							                      																																																																																						                      																																																																																					                      																																																																																				                      																																																																																			                      																																																																																		                      																																																																																	                      																																																																																                      																																																																															                      																																																																														                      																																																																													                      																																																																												                      																																																																											                      																																																																										                      																																																																									                      																																																																								                      																																																																							                      																																																																						                      																																																																					                      																																																																				                      																																																																			                      																																																																		                      																																																																	                      																																																																                      																																																															                      																																																														                      																																																													                      																																																												                      																																																											                      																																																										                      																																																									                      																																																								                      																																																							                      																																																						                      																																																					                      																																																				                      																																																			                      																																																		                      																																																	                      																																																                      																																															                      																																														                      																																													                      																																												                      																																											                      																																										                      																																									                       <?php selected('paused', $subscription_status); ?>>Paused</option>
																																																																																												<option value="unpaid"																																																																																												                      																																																																																											                      																																																																																										                      																																																																																									                      																																																																																								                      																																																																																							                      																																																																																						                      																																																																																					                      																																																																																				                      																																																																																			                      																																																																																		                      																																																																																	                      																																																																																                      																																																																															                      																																																																														                      																																																																													                      																																																																												                      																																																																											                      																																																																										                      																																																																									                      																																																																								                      																																																																							                      																																																																						                      																																																																					                      																																																																				                      																																																																			                      																																																																		                      																																																																	                      																																																																                      																																																															                      																																																														                      																																																													                      																																																												                      																																																											                      																																																										                      																																																									                      																																																								                      																																																							                      																																																						                      																																																					                      																																																				                      																																																			                      																																																		                      																																																	                      																																																                      																																															                      																																														                      																																													                      																																												                      																																											                      																																										                      																																									                       <?php selected('unpaid', $subscription_status); ?>>Unpaid</option>
																																																																																												<option value="trialing"																																																																																												                        																																																																																											                        																																																																																										                        																																																																																									                        																																																																																								                        																																																																																							                        																																																																																						                        																																																																																					                        																																																																																				                        																																																																																			                        																																																																																		                        																																																																																	                        																																																																																                        																																																																															                        																																																																														                        																																																																													                        																																																																												                        																																																																											                        																																																																										                        																																																																									                        																																																																								                        																																																																							                        																																																																						                        																																																																					                        																																																																				                        																																																																			                        																																																																		                        																																																																	                        																																																																                        																																																															                        																																																														                        																																																													                        																																																												                        																																																											                        																																																										                        																																																									                        																																																								                        																																																							                        																																																						                        																																																					                        																																																				                        																																																			                        																																																		                        																																																	                        																																																                        																																															                        																																														                        																																													                        																																												                        																																											                        																																										                        																																									                         <?php selected('trialing', $subscription_status); ?>>Trialing</option>
																																																																																												</select>
																																																																																												<span class="chama-subscription-status edit-status"><span id="chama-subscription-status" class="chama-subscription-status-icon																																																																																												                                                                                                                              																																																																																											                                                                                                                              																																																																																										                                                                                                                              																																																																																									                                                                                                                              																																																																																								                                                                                                                              																																																																																							                                                                                                                              																																																																																						                                                                                                                              																																																																																					                                                                                                                              																																																																																				                                                                                                                              																																																																																			                                                                                                                              																																																																																		                                                                                                                              																																																																																	                                                                                                                              																																																																																                                                                                                                              																																																																															                                                                                                                              																																																																														                                                                                                                              																																																																													                                                                                                                              																																																																												                                                                                                                              																																																																											                                                                                                                              																																																																										                                                                                                                              																																																																									                                                                                                                              																																																																								                                                                                                                              																																																																							                                                                                                                              																																																																						                                                                                                                              																																																																					                                                                                                                              																																																																				                                                                                                                              																																																																			                                                                                                                              																																																																		                                                                                                                              																																																																	                                                                                                                              																																																																                                                                                                                              																																																															                                                                                                                              																																																														                                                                                                                              																																																													                                                                                                                              																																																												                                                                                                                              																																																											                                                                                                                              																																																										                                                                                                                              																																																									                                                                                                                              																																																								                                                                                                                              																																																							                                                                                                                              																																																						                                                                                                                              																																																					                                                                                                                              																																																				                                                                                                                              																																																			                                                                                                                              																																																		                                                                                                                              																																																	                                                                                                                              																																																                                                                                                                              																																															                                                                                                                              																																														                                                                                                                              																																													                                                                                                                              																																												                                                                                                                              																																											                                                                                                                              																																										                                                                                                                              																																									                                                                                                                               <?php echo esc_attr($subscription_status); ?>"></span></span>
																																																																																												<br/>
																																																																																												<br/>

																																																																																																																				                                </div>


																																																																																																																				<div class="chama-admin-box">

																																																																																																																													<div id="major-publishing-actions">
																																																																																																																														<div id="publishing-action">
																																																																																												                                                                                                                                        <?php if ($subscription_status === 'incomplete' || $subscription_status === 'past_due' || $subscription_status === 'unpaid'): ?>
																																																																																												                                                                                                                                               <div class="chama-sync-subscription-wrapper">
																																																																																												                                                                                                                                               <a href="<?php echo esc_url($sync_subscription_url); ?>"  class="button-secondary chama-sync-subscription">Sync Subscription</a>
																																																																																												                                                                                                                                               </div>

																																																																																												                                                                                                                                                <?php endif; ?>
																																			<input type="submit" class="button button-primary right" value="Save Subscription">

																		                                                                    <?php if ($payment_status === 'complete'): ?>
																																			<a href="<?php echo esc_url($resend_receipt_url); ?>"  class="button-secondary  right chama-resend-receipt">Resend Receipt</a>
															                                                                                <?php else: ?>
                                                                                    <a href="<?php echo esc_url($sync_payment_url); ?>"  class="button-secondary right chama-sync-payment">Sync Payment</a>
			                                                                    <?php endif; ?>									</div>
																<div class="clear"></div>
															</div>

														</div>
								<?php
                                    }
                                    endif;
                                    //renewals metabox
                                    add_action('admin_init', 'chama_subscription_renewals_metabox');
                                    if (! function_exists('chama_subscription_renewals_metabox')):
                                        function chama_subscription_renewals_metabox()
                                    {
                                            add_meta_box(
                                                'chama_stripe_renewals',
                                                __('Synchronize Renewals', 'chama'),
                                                'chama_subscription_renewals_display_metabox',
                                                'chama_subscription',
                                                "side",
                                                "default"
                                            );

                                        }
                                    endif;
                                    //Displaying the meta box
                                    if (! function_exists('chama_subscription_renewals_display_metabox')):
                                        function chama_subscription_renewals_display_metabox($post)
                                    {
                                            $nonce                  = wp_create_nonce('chama_sync_subscription_renewals_nonce');
                                            $subscription_status    = get_post_meta($post->ID, 'chama_subscription_status', true);
                                            $stripe_subscription_id = get_post_meta($post->ID, 'chama_stripe_subscription_id', true);

                                        ?>
																																																																																																																				        <div class="chama-publish-header">
																																																																																																																													<h3 class="hndle">Synchronize Renewals</h3>

																																																																																																																				                                </div>
																																																																																																																				                                <div class="chama-publish-box-inside ">

																																																																																																																				                               <div>
																																																																																	                                                                                                                                                                            <p>To synchronize subcription renewals related to this transaction from <strong>Stripe</strong>, click the button below. Note that you can only synchronize as far back as the last 12 transactions on the subscripition.</p>
																																																																																	                                                                                                                                                                           </div>
																																																																																																																				       <div class="chama-response chama-renewal-response">

																																																																																	                                                                                                                                                   </div>
																																																																																																																				                                </div>

																																																																																																																				<div class="chama-admin-box">

																																																																																																																													<div id="chama-subscription-renewal-actions" class="chama_metabox-actions">



																																																																																												                                                                                                                                               <a href="javascript:;" data-nonce='<?php echo esc_attr($nonce); ?>' data-id="<?php echo esc_attr($post->ID); ?>" data-stripe-subscription-id="<?php echo esc_attr($stripe_subscription_id); ?>" class="button-secondary chama-sync-subscription-renewals button<?php echo $subscription_status === 'active' ? '' : 'disabled' ?>">Sync Renewals</a>
																																																																																                                                                                                                                                                                               <span id="chama-sync-renewals-spinner" class="chama-admin-spinner"><img src="<?php echo esc_attr(admin_url('/images/spinner.gif')); ?>" ></span>




																																																																																												<div class="clear"></div>
																																																																																											</div>

																																																																																										</div>
																																																																																				<?php
                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                    endif;
                                                                                                                                                                                                                                                                                                                                                    //cancelation metabox
                                                                                                                                                                                                                                                                                                                                                    add_action('admin_init', 'chama_cancel_subscription_metabox');
                                                                                                                                                                                                                                                                                                                                                    if (! function_exists('chama_cancel_subscription_metabox')):
                                                                                                                                                                                                                                                                                                                                                        function chama_cancel_subscription_metabox()
                                                                                                                                                                                                                                                                                                                                                    {
                                                                                                                                                                                                                                                                                                                                                            add_meta_box(
                                                                                                                                                                                                                                                                                                                                                                'chama_stripe_cancelation',
                                                                                                                                                                                                                                                                                                                                                                __('Cancel Subscription', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                'chama_cancel_subscription_display_metabox',
                                                                                                                                                                                                                                                                                                                                                                'chama_subscription',
                                                                                                                                                                                                                                                                                                                                                                "side",
                                                                                                                                                                                                                                                                                                                                                                "default"
                                                                                                                                                                                                                                                                                                                                                            );

                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                    endif;
                                                                                                                                                                                                                                                                                                                                                    //Displaying the meta box
                                                                                                                                                                                                                                                                                                                                                    if (! function_exists('chama_cancel_subscription_display_metabox')):
                                                                                                                                                                                                                                                                                                                                                        function chama_cancel_subscription_display_metabox($post)
                                                                                                                                                                                                                                                                                                                                                    {
                                                                                                                                                                                                                                                                                                                                                            $nonce                  = wp_create_nonce('chama_cancel_subscription_nonce');
                                                                                                                                                                                                                                                                                                                                                            $subscription_status    = get_post_meta($post->ID, 'chama_subscription_status', true);
                                                                                                                                                                                                                                                                                                                                                            $stripe_subscription_id = get_post_meta($post->ID, 'chama_stripe_subscription_id', true);
                                                                                                                                                                                                                                                                                                                                                        ?>
																																																																																																																					        <div class="chama-publish-header">
																																																																																																																														<h3 class="hndle">Cancel Subscription</h3>

																																																																																																																					                                </div>
																																																																																																																					                                <div class="chama-publish-box-inside ">

																																																																																																																					                               <div>
																																																																																		                                                                                                                                                                            <p>To cancel this subcription on <strong>Stripe</strong>, click the button below. Note that this action cannot be undone.</p>
																																																																																		                                                                                                                                                                           </div>

																																																																												                                                                                                                                                                           <!--modal-->
																																																																												                                                                                                                                                                           <?php add_thickbox(); ?>




																																																																												<div id="chama-admin-modal" style="display:none;">
																																																																												    <h2><span class="dashicons dashicons-warning"></span> Cancel Subscription</h2>
																																																																												    <p>Are you sure you want to cancel this subscription?</p>
																																																																												    <div class="chama-admin-processing"> <img src="<?php echo esc_attr(admin_url('/images/spinner.gif')); ?>" > Processing...</div>
																																																																												    <div class="chama-response chama-cancelation-response">

																																																																												</div>
																																																																												    <div class="chama-admin-modal-actions">
																																																																												    <button class="chama-close-admin-modal button button-secondary"  aria-label="Close this dialog window">Nevermind</button>
																																																																												    <button class="chama-admin-process-cancelation button button-primary chama-primary-button" data-nonce='<?php echo esc_attr($nonce); ?>' data-id="<?php echo esc_attr($post->ID); ?>" data-stripe-subscription-id="<?php echo esc_attr($stripe_subscription_id); ?>">Yes, cancel</button>
																																																																												    </div>
																																																																												</div>
																																																																												                                                                                                                                                                           <!--modal-->
																																																																																																																					                                </div>

																																																																																																																					<div class="chama-admin-box">

																																																																												                                                                                                                                                                    <div id="chama-cancel_subscrption-actions" class="chama_metabox-actions">



																																																																																													                                                                                                                                               <a href="#TB_inline?width=400&height=200&inlineId=chama-admin-modal" class="thickbox chama-button-danger chama-cancel-subscription button																																																																																													                                                                                                                                                                                                                                                                                        																																																																																												                                                                                                                                                                                                                                                                                        																																																																																											                                                                                                                                                                                                                                                                                        																																																																																										                                                                                                                                                                                                                                                                                        																																																																																									                                                                                                                                                                                                                                                                                        																																																																																								                                                                                                                                                                                                                                                                                        																																																																																							                                                                                                                                                                                                                                                                                        																																																																																						                                                                                                                                                                                                                                                                                        																																																																																					                                                                                                                                                                                                                                                                                        																																																																																				                                                                                                                                                                                                                                                                                        																																																																																			                                                                                                                                                                                                                                                                                        																																																																																		                                                                                                                                                                                                                                                                                        																																																																																	                                                                                                                                                                                                                                                                                        																																																																																                                                                                                                                                                                                                                                                                        																																																																															                                                                                                                                                                                                                                                                                        																																																																														                                                                                                                                                                                                                                                                                        																																																																													                                                                                                                                                                                                                                                                                        																																																																												                                                                                                                                                                                                                                                                                        																																																																											                                                                                                                                                                                                                                                                                        																																																																										                                                                                                                                                                                                                                                                                        																																																																									                                                                                                                                                                                                                                                                                        																																																																								                                                                                                                                                                                                                                                                                        																																																																							                                                                                                                                                                                                                                                                                        																																																																						                                                                                                                                                                                                                                                                                        																																																																					                                                                                                                                                                                                                                                                                        																																																																				                                                                                                                                                                                                                                                                                        																																																																			                                                                                                                                                                                                                                                                                        																																																																		                                                                                                                                                                                                                                                                                        																																																																	                                                                                                                                                                                                                                                                                        																																																																                                                                                                                                                                                                                                                                                        																																																															                                                                                                                                                                                                                                                                                        																																																														                                                                                                                                                                                                                                                                                        																																																													                                                                                                                                                                                                                                                                                        																																																												                                                                                                                                                                                                                                                                                        																																																											                                                                                                                                                                                                                                                                                        																																																										                                                                                                                                                                                                                                                                                        																																																									                                                                                                                                                                                                                                                                                        																																																								                                                                                                                                                                                                                                                                                        																																																							                                                                                                                                                                                                                                                                                        																																																						                                                                                                                                                                                                                                                                                        																																																					                                                                                                                                                                                                                                                                                        																																																				                                                                                                                                                                                                                                                                                        																																																			                                                                                                                                                                                                                                                                                        																																																		                                                                                                                                                                                                                                                                                        																																																	                                                                                                                                                                                                                                                                                        																																																                                                                                                                                                                                                                                                                                        																																															                                                                                                                                                                                                                                                                                        																																														                                                                                                                                                                                                                                                                                        																																													                                                                                                                                                                                                                                                                                        																																												                                                                                                                                                                                                                                                                                        																																											                                                                                                                                                                                                                                                                                        																																										                                                                                                                                                                                                                                                                                         <?php echo $subscription_status === 'active' ? '' : 'disabled' ?>">Cancel Subscription</a>





																																																																																												<div class="clear"></div>
																																																																																											</div>

																																																																																										</div>
																																																																																				<?php
                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                    endif;
                                                                                                                                                                                                                                                                                                                                                    //stripe metabox
                                                                                                                                                                                                                                                                                                                                                    add_action('admin_init', 'chama_subscription_stripe_meta_metabox');
                                                                                                                                                                                                                                                                                                                                                    if (! function_exists('chama_subscription_stripe_meta_metabox')):
                                                                                                                                                                                                                                                                                                                                                        function chama_subscription_stripe_meta_metabox()
                                                                                                                                                                                                                                                                                                                                                    {
                                                                                                                                                                                                                                                                                                                                                            add_meta_box(
                                                                                                                                                                                                                                                                                                                                                                'stripe_meta',
                                                                                                                                                                                                                                                                                                                                                                __('Stripe Meta', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                'chama_subscription_stripe_meta_display_metabox',
                                                                                                                                                                                                                                                                                                                                                                'chama_subscription',
                                                                                                                                                                                                                                                                                                                                                                "side",
                                                                                                                                                                                                                                                                                                                                                                "default"
                                                                                                                                                                                                                                                                                                                                                            );

                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                    endif;
                                                                                                                                                                                                                                                                                                                                                    //Displaying the meta box
                                                                                                                                                                                                                                                                                                                                                    if (! function_exists('chama_subscription_stripe_meta_display_metabox')):
                                                                                                                                                                                                                                                                                                                                                        function chama_subscription_stripe_meta_display_metabox($post)
                                                                                                                                                                                                                                                                                                                                                    {
                                                                                                                                                                                                                                                                                                                                                            $stripe_customer_id       = get_post_meta($post->ID, 'chama_stripe_customer_id', true);
                                                                                                                                                                                                                                                                                                                                                            $stripe_payment_intent_id = get_post_meta($post->ID, 'chama_stripe_payment_intent_id', true);
                                                                                                                                                                                                                                                                                                                                                            $stripe_subscription_id   = get_post_meta($post->ID, 'chama_stripe_subscription_id', true);
                                                                                                                                                                                                                                                                                                                                                        ?>
																																																																																																																				<p>
																																																																																																																				<strong>Transaction ID:</strong><br>
																																																																																																																				<?php echo esc_html($stripe_payment_intent_id); ?>
																																																																																																																				</p>
																																																																																																																				<p>
																																																																																																																				<strong>Customer ID:</strong><br>
																																																																																																																				<?php echo esc_html($stripe_customer_id); ?>
																																																																																																																				</p>
																																																																																												                                                                                                <p>
																																																																																																																				<strong>Subscription ID:</strong><br>
																																																																																																																				<?php echo esc_html($stripe_subscription_id); ?>
																																																																																												</p>
																																																																																																																						<?php
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            endif;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //This function saves the data you put in the meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            if (! function_exists('chama_subscription_save_postdata')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                function chama_subscription_save_postdata($post_id, $post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    if ((isset($_POST['chama_publish_nonce'])) && $post->post_type == 'chama_subscription') {

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        //Not save if the user hasn't submitted changes
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        // Verifying whether input is coming from the proper form

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $nonce = isset($_POST['chama_publish_nonce']) ? sanitize_text_field(wp_unslash($_POST['chama_publish_nonce'])) : '';

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if (! $nonce || ! wp_verify_nonce($nonce, 'publish_metadata')) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        // Making sure the user has permission

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if (! current_user_can('edit_post', $post_id)) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if (! empty($_POST['chama_subscription_total_amount'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            /* Get the posted data and sanitize it for use as an HTML class. */
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $data = str_replace(',', '', number_format(floatval($_POST['chama_subscription_total_amount']), 2));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            update_post_meta($post_id, 'chama_subscription_total_amount', $data);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            update_post_meta($post_id, 'chama_subscription_total_amount', 0);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if (! empty($_POST['chama_payment_status'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            /* Get the posted data and sanitize it for use as an HTML class. */
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $data = sanitize_text_field(wp_unslash($_POST['chama_payment_status']));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            update_post_meta($post_id, 'chama_payment_status', $data);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            update_post_meta($post_id, 'chama_payment_status', 'processing');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        //update subscription status
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if (! empty($_POST['chama_subscription_status'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            /* Get the posted data and sanitize it for use as an HTML class. */
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $data = sanitize_text_field(wp_unslash($_POST['chama_subscription_status']));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            update_post_meta($post_id, 'chama_subscription_status', $data);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            update_post_meta($post_id, 'chama_subscription_status', 'incomplete');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_action('save_post_chama_subscription', 'chama_subscription_save_postdata', 20, 2);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //show columns
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_filter('manage_chama_subscription_posts_columns', 'chama_subscription_columns');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_subscription_columns($columns)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                unset($columns['date']);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns = [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'cb'                  => $columns['cb'],
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'member'              => __('Member', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'tier'                => __('Tier', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'status'              => __('Payment Status', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'subscription_status' => __('Subscription Status', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'transaction'         => __('Transaction', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'payment_date'        => __('Payment Date', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'renewal_date'        => __('Renewal Date', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'amount'              => __('Amount', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'details'             => __('Details', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                return $columns;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_filter('manage_chama_subscription_posts_custom_column', 'chama_populate_subscription_columns', 10, 2);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_populate_subscription_columns($column, $id)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                switch ($column) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    case 'amount':

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $currency = get_post_meta($id, 'chama_subscription_currency', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $amount   = get_post_meta($id, 'chama_subscription_total_amount', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        echo esc_html(CHAMA_SYMBOL . number_format((float) $amount, 2, '.', ''));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    case 'payment_date':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $subscription_date           = get_post_meta($id, 'chama_subscription_date_time', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $subscription_date_formatted = gmdate("F, j, Y", strtotime($subscription_date));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        echo esc_html($subscription_date_formatted);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    case 'renewal_date':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $renewal_date = get_post_meta($id, 'chama_subscription_renewal_date', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if ($renewal_date) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            echo esc_html($renewal_date);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    case 'details':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $edit_link = get_edit_post_link($id);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        echo '<a href="' . esc_url($edit_link) . '" class="button button-small hint--top hint--bounce"><span class="dashicons dashicons-visibility"></span></a>';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    case 'status':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $test_mode        = get_post_meta($id, 'chama_is_test_subscription', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $test_mode_notice = $test_mode ? '<span  class="chama-test-mode-label label-listing" aria-label="This subscription was made in test mode." rel="tooltip">Test</span>' : '';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $status           = get_post_meta($id, 'chama_payment_status', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        echo '<span class="chama-payment-status listing-status"><span class="chama-payment-status-icon ' . esc_attr($status) . '"></span>&nbsp;' . esc_html(ucwords($status)) . '</span> ' . wp_kses($test_mode_notice, chama_allowed_html());
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    case 'subscription_status':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $subscription_status = get_post_meta($id, 'chama_subscription_status', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        echo '<span class="chama-subscription-status listing-status"><span class="chama-subscription-status-icon ' . esc_attr($subscription_status) . '"></span>&nbsp;' . esc_html(ucwords($subscription_status)) . '</span> ';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    case 'transaction':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $subscription_event = get_post_meta($id, 'chama_subscription_event', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        echo '<span class="chama-subscription-status listing-status"><span class="chama-subscription-status-icon ' . esc_attr($subscription_event) . '"></span>&nbsp;' . esc_html(ucwords($subscription_event)) . '</span> ';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    case 'member':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $user_id        = get_post_meta($id, 'chama_user_id', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $user           = $user_id ? get_user_by('id', $user_id) : false;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $user_full_name = $user && $user->first_name && $user->last_name ? $user->first_name . ' ' . $user->last_name : false;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $user_email     = $user ? $user->user_email : '';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $display_name   = $user_full_name ? $user_full_name : $user_email;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $user_link      = admin_url('user-edit.php?user_id=') . $user_id;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        echo '<a href="' . esc_url($user_link) . '">' . esc_html($user_id) . ' - ' . esc_html($display_name) . '</a>';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    case 'tier':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $tier_id            = get_post_meta($id, 'chama_subscription_item_id', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $description        = get_post_meta($id, 'chama_subscription_description', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $subscriptions_link = admin_url('edit.php?post_type=chama_subscription');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $chama_filter_nonce = wp_create_nonce('chama_filter_nonce');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $subscriptions_link = esc_url(add_query_arg(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                'tid' => $tier_id,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                'cfn' => $chama_filter_nonce,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ],
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $subscriptions_link
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        echo '<a href="' . esc_url($subscriptions_link) . '">' . esc_html($description) . '</a>';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    default:
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //Extend search for subscriptions(description)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_action('admin_init', 'chama_subscription_extend_search');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_subscription_extend_search()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                global $typenow;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if ($typenow === 'chama_subscription') {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    add_filter('posts_search', 'chama_subscription_search_override', 10, 2);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_subscription_search_override($search, $query)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                global $wpdb;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if ($query->is_main_query() && ! empty($query->query['s'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $like   = '%' . $wpdb->esc_like($query->query['s']) . '%';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $search = preg_replace("#\({$wpdb->posts}.post_title LIKE [^)]+\)\K#",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $wpdb->prepare("
or exists (
select * from {$wpdb->postmeta} where post_id={$wpdb->posts}.ID
and meta_key in ('chama_user_id', 'chama_email', 'chama_full_name' , 'chama_subscription_description' , 'chama_subscription_event' , 'chama_subscription_status')
and meta_value like %s
)
", $like), $search);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                return $search;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //Filter subscriptions
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_action('parse_query', 'chama_filter_subscriptions');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_filter_subscriptions($query)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                global $pagenow;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if (! is_admin() || 'edit.php' !== $pagenow || ! isset($query->query['post_type']) || 'chama_subscription' !== $query->query['post_type']) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $nonce = '';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if (isset($_GET['cfn'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $nonce = sanitize_text_field(wp_unslash($_GET['cfn']));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $user_id                = isset($_GET['uid']) ? sanitize_text_field(wp_unslash($_GET['uid'])) : '';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $tier_id                = isset($_GET['tid']) ? sanitize_text_field(wp_unslash($_GET['tid'])) : '';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $stripe_subscription_id = isset($_GET['sid']) ? sanitize_text_field(wp_unslash($_GET['sid'])) : '';

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $nonce_is_valid = $nonce && wp_verify_nonce($nonce, 'chama_filter_nonce');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $has_values     = $user_id || $tier_id || $stripe_subscription_id;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if (! $nonce_is_valid || ! $has_values) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $meta_query_args = [];

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if ($user_id) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $meta_query_args[] = [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'key'   => 'chama_user_id',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'value' => $user_id,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $meta_query_args[] = [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'key'   => 'chama_subscription_event',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'value' => 'creation',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if ($tier_id) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $meta_query_args[] = [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'key'   => 'chama_subscription_item_id',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'value' => $tier_id,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if ($stripe_subscription_id) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $meta_query_args[] = [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'key'   => 'chama_stripe_subscription_id',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'value' => $stripe_subscription_id,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if ($query->is_main_query() && $meta_query_args) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $query->set('meta_query', $meta_query_args);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            /*=============================================FUND=============================================*/
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            /**
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             * Add Fund metaboxes
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             */
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_action('admin_init', 'chama_fund_information_metabox');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            if (! function_exists('chama_fund_information_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                function chama_fund_information_metabox()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    add_meta_box(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'fund_information',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        __('Fund Information', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'chama_fund_information_display_metabox',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'chama_fund',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "normal",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "high"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    );

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //Displaying the meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            if (! function_exists('chama_fund_information_display_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                function chama_fund_information_display_metabox($post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $fund_date           = get_post_meta($post->ID, 'chama_fund_date_time', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $fund_date_formatted = gmdate("F, j, Y H:i:s", strtotime($fund_date));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $fund_total          = get_post_meta($post->ID, 'chama_fund_total_amount', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $fund_total_display  = CHAMA_SYMBOL . number_format((float) $fund_total, 2, '.', '');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $campaign_title      = get_post_meta($post->ID, 'chama_fund_description', true);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ?>
																																																																																																																				        <h2 class="chama-metabox-heading">
																																																																																																																				        Fund Information				</h2>
																																																																																																																						<div class="chama-metabox-row">
																																																																																																																						<div class="chama-metabox-col">
																																																																																																																						<p>
																																																																																																																																		<strong>Transaction Date:</strong><br>
																																																																																																																																		<?php echo esc_html($fund_date_formatted); ?>										</p>
																																																																																																																						</div>
																																																																																																																						<div class="chama-metabox-col">
																																																																																																																						<p>
																																																																																																																																		<strong>Total Amount:</strong><br>
																																																																																																																																		<?php echo esc_html($fund_total_display); ?>												</p>
																																																																																																																						</div>
																																																																																																											                                            <div class="chama-metabox-col">
																																																																																																																						<p>
																																																																																																																																		<strong>Campaign:</strong><br>
																																																																																																																																		<?php echo esc_html($campaign_title); ?>												</p>
																																																																																																																						</div>
																																																																																																																						</div>

																																																																																																																						<?php
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            endif;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_action('admin_init', 'chama_funder_details_metabox');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            if (! function_exists('chama_funder_details_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                function chama_funder_details_metabox()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    add_meta_box(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'funder_details',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        __('Funder Details', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'chama_funder_details_display_metabox',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'chama_fund',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "normal",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "high"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    );

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //Displaying the meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            if (! function_exists('chama_funder_details_display_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                function chama_funder_details_display_metabox($post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $user_id        = get_post_meta($post->ID, 'chama_user_id', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $user           = $user_id ? get_user_by('id', $user_id) : false;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $user_full_name = $user ? $user->first_name . ' ' . $user->last_name : '';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $user_email     = $user ? $user->user_email : '';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $funds_link     = admin_url('edit.php?post_type=chama_fund');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $user_link      = admin_url('user-edit.php?user_id=') . $user_id;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    // Create account disconnect URL
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $funds_link = esc_url(add_query_arg(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'uid' => $user_id,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ],
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $funds_link
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ?>
																																																																																																																				         <h2 class="chama-metabox-heading">
																																																																																																																				         Funder Details				</h2>
																																																																																																																						<div class="chama-metabox-row">
																																																																																																																						<div class="chama-metabox-col">
																																																																																																																						<p>
																																																																																																																																		<strong>Funder ID:</strong><br>
																																																																																																																																		<?php echo esc_html($user_id); ?><br/>
																																																																																																														                                                                                <a href="<?php echo esc_url($user_link) ?>">User profile</a><br/>
																																																																																																															                                                                            <a href="<?php echo esc_url($funds_link) ?>">All funds for this funder</a>										</p>
																																																																																																																						</div>
																																																																																																																						<div class="chama-metabox-col">
																																																																																																																						<p>
																																																																																																																																		<strong>Funder Name:</strong><br>
																																																																																																																																		<?php echo esc_html($user_full_name); ?>												</p>
																																																																																																																						</div>
																																																																																																																				        <div class="chama-metabox-col">
																																																																																																																						<p>
																																																																																																																																		<strong>Funder Email:</strong><br>
																																																																																																																																		<?php echo esc_html($user_email); ?>												</p>
																																																																																																																						</div>
																																																																																																																						</div>

																																																																																																																						<?php
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //publish metabox
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_action('admin_init', 'chama_fund_publish_metabox');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            if (! function_exists('chama_fund_publish_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                function chama_fund_publish_metabox()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    add_meta_box(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'submitdiv',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        __('Update Fund', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'chama_fund_publish_display_metabox',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'chama_fund',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "side",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "default"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    );

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //Displaying the meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            if (! function_exists('chama_fund_publish_display_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                function chama_fund_publish_display_metabox($post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $delete_url         = get_delete_post_link($post->ID, '', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $status             = get_post_meta($post->ID, 'chama_payment_status', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $current_url        = home_url(chama_get_current_request_uri());
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $resend_receipt_url = esc_url(add_query_arg(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'id'           => (int) $post->ID,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'chama_nonce'  => wp_create_nonce('chama_resend_receipt_nonce'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'chama_action' => 'resend-receipt',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'type'         => 'crowdfunding',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ],
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $current_url
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $sync_payment_url = esc_url(add_query_arg(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'id'           => (int) $post->ID,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'chama_nonce'  => wp_create_nonce('chama_sync_payment_nonce'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'chama_action' => 'sync-payment',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ],
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $current_url
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ));

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    add_query_arg('account-updated', 'true', $current_url);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ?>
																																																																																																																				        <div class="chama-publish-header">
																																																																																																																													<h3 class="hndle">Update Fund</h3>

																																																																																																																													<span class="delete-payment"><a title="Delete Fund" class="delete-single-payment delete-payment-button dashicons dashicons-trash" href="<?php echo esc_url($delete_url) ?>" aria-label="Delete Fund"></a></span>
																																																																																																																				                                </div>
																																																																																																																				                                <div class="chama-publish-box-inside ">

																																																																																																																				                                <?php
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        wp_nonce_field('publish_metadata', 'chama_publish_nonce');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            chama_price_display_metabox($post->ID, 'chama_fund_total_amount', '', 'Total Amount');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ?>
																																																																																																																				        <div>
																																																																																																																				            <label><strong>Status:</strong></label>
																																																																																																																				        </div>
																																																																																																																				         <select name="chama_payment_status" id="chama_payment_status"  >

																																																																																																																				                                        <option value="abandoned"																																																																																																																				                                                                 																																																																																																																			                                                                 																																																																																																																		                                                                 																																																																																																																	                                                                 																																																																																																																                                                                 																																																																																																															                                                                 																																																																																																														                                                                 																																																																																																													                                                                 																																																																																																												                                                                 																																																																																																											                                                                 																																																																																																										                                                                 																																																																																																									                                                                 																																																																																																								                                                                 																																																																																																							                                                                 																																																																																																						                                                                 																																																																																																					                                                                 																																																																																																				                                                                 																																																																																																			                                                                 																																																																																																		                                                                 																																																																																																	                                                                 																																																																																																                                                                 																																																																																															                                                                 																																																																																														                                                                 																																																																																													                                                                 																																																																																												                                                                 																																																																																											                                                                 																																																																																										                                                                 																																																																																									                                                                 																																																																																								                                                                 																																																																																							                                                                 																																																																																						                                                                 																																																																																					                                                                 																																																																																				                                                                 																																																																																			                                                                 																																																																																		                                                                 																																																																																	                                                                 																																																																																                                                                 																																																																															                                                                 																																																																														                                                                 																																																																													                                                                 																																																																												                                                                 																																																																											                                                                 																																																																										                                                                 																																																																									                                                                 																																																																								                                                                 																																																																							                                                                 																																																																						                                                                 																																																																					                                                                 																																																																				                                                                 																																																																			                                                                 																																																																		                                                                 																																																																	                                                                  <?php selected('abandoned', $status); ?>>Abandoned</option>
																																																																																																																				                                        <option value=" canceled"																																																																																																																				                                                                 																																																																																																																			                                                                 																																																																																																																		                                                                 																																																																																																																	                                                                 																																																																																																																                                                                 																																																																																																															                                                                 																																																																																																														                                                                 																																																																																																													                                                                 																																																																																																												                                                                 																																																																																																											                                                                 																																																																																																										                                                                 																																																																																																									                                                                 																																																																																																								                                                                 																																																																																																							                                                                 																																																																																																						                                                                 																																																																																																					                                                                 																																																																																																				                                                                 																																																																																																			                                                                 																																																																																																		                                                                 																																																																																																	                                                                 																																																																																																                                                                 																																																																																															                                                                 																																																																																														                                                                 																																																																																													                                                                 																																																																																												                                                                 																																																																																											                                                                 																																																																																										                                                                 																																																																																									                                                                 																																																																																								                                                                 																																																																																							                                                                 																																																																																						                                                                 																																																																																					                                                                 																																																																																				                                                                 																																																																																			                                                                 																																																																																		                                                                 																																																																																	                                                                 																																																																																                                                                 																																																																															                                                                 																																																																														                                                                 																																																																													                                                                 																																																																												                                                                 																																																																											                                                                 																																																																										                                                                 																																																																									                                                                 																																																																								                                                                 																																																																							                                                                 																																																																						                                                                 																																																																					                                                                 																																																																				                                                                 																																																																			                                                                 																																																																		                                                                 																																																																	                                                                  <?php selected(' canceled', $status); ?>> Canceled</option>
																																																																																																																				                                        <option value="complete"																																																																																																																				                                                                																																																																																																																			                                                                																																																																																																																		                                                                																																																																																																																	                                                                																																																																																																																                                                                																																																																																																															                                                                																																																																																																														                                                                																																																																																																													                                                                																																																																																																												                                                                																																																																																																											                                                                																																																																																																										                                                                																																																																																																									                                                                																																																																																																								                                                                																																																																																																							                                                                																																																																																																						                                                                																																																																																																					                                                                																																																																																																				                                                                																																																																																																			                                                                																																																																																																		                                                                																																																																																																	                                                                																																																																																																                                                                																																																																																															                                                                																																																																																														                                                                																																																																																													                                                                																																																																																												                                                                																																																																																											                                                                																																																																																										                                                                																																																																																									                                                                																																																																																								                                                                																																																																																							                                                                																																																																																						                                                                																																																																																					                                                                																																																																																				                                                                																																																																																			                                                                																																																																																		                                                                																																																																																	                                                                																																																																																                                                                																																																																															                                                                																																																																														                                                                																																																																													                                                                																																																																												                                                                																																																																											                                                                																																																																										                                                                																																																																									                                                                																																																																								                                                                																																																																							                                                                																																																																						                                                                																																																																					                                                                																																																																				                                                                																																																																			                                                                																																																																		                                                                																																																																	                                                                 <?php selected('complete', $status); ?>>Complete</option>
																																																																																																																				                                        <option value="failed"																																																																																																																				                                                              																																																																																																																			                                                              																																																																																																																		                                                              																																																																																																																	                                                              																																																																																																																                                                              																																																																																																															                                                              																																																																																																														                                                              																																																																																																													                                                              																																																																																																												                                                              																																																																																																											                                                              																																																																																																										                                                              																																																																																																									                                                              																																																																																																								                                                              																																																																																																							                                                              																																																																																																						                                                              																																																																																																					                                                              																																																																																																				                                                              																																																																																																			                                                              																																																																																																		                                                              																																																																																																	                                                              																																																																																																                                                              																																																																																															                                                              																																																																																														                                                              																																																																																													                                                              																																																																																												                                                              																																																																																											                                                              																																																																																										                                                              																																																																																									                                                              																																																																																								                                                              																																																																																							                                                              																																																																																						                                                              																																																																																					                                                              																																																																																				                                                              																																																																																			                                                              																																																																																		                                                              																																																																																	                                                              																																																																																                                                              																																																																															                                                              																																																																														                                                              																																																																													                                                              																																																																												                                                              																																																																											                                                              																																																																										                                                              																																																																									                                                              																																																																								                                                              																																																																							                                                              																																																																						                                                              																																																																					                                                              																																																																				                                                              																																																																			                                                              																																																																		                                                              																																																																	                                                               <?php selected('failed', $status); ?>>Failed</option>
																																																																																																																												        <option value="processing"																																																																																																																												                                  																																																																																																																											                                  																																																																																																																										                                  																																																																																																																									                                  																																																																																																																								                                  																																																																																																																							                                  																																																																																																																						                                  																																																																																																																					                                  																																																																																																																				                                  																																																																																																																			                                  																																																																																																																		                                  																																																																																																																	                                  																																																																																																																                                  																																																																																																															                                  																																																																																																														                                  																																																																																																													                                  																																																																																																												                                  																																																																																																											                                  																																																																																																										                                  																																																																																																									                                  																																																																																																								                                  																																																																																																							                                  																																																																																																						                                  																																																																																																					                                  																																																																																																				                                  																																																																																																			                                  																																																																																																		                                  																																																																																																	                                  																																																																																																                                  																																																																																															                                  																																																																																														                                  																																																																																													                                  																																																																																												                                  																																																																																											                                  																																																																																										                                  																																																																																									                                  																																																																																								                                  																																																																																							                                  																																																																																						                                  																																																																																					                                  																																																																																				                                  																																																																																			                                  																																																																																		                                  																																																																																	                                  																																																																																                                  																																																																															                                  																																																																														                                  																																																																													                                  																																																																												                                  																																																																											                                  																																																																										                                  																																																																									                                   <?php selected('processing', $status); ?>>Processing</option>
																																																																																																																				                                        <option value="refunded"																																																																																																																				                                                                																																																																																																																			                                                                																																																																																																																		                                                                																																																																																																																	                                                                																																																																																																																                                                                																																																																																																															                                                                																																																																																																														                                                                																																																																																																													                                                                																																																																																																												                                                                																																																																																																											                                                                																																																																																																										                                                                																																																																																																									                                                                																																																																																																								                                                                																																																																																																							                                                                																																																																																																						                                                                																																																																																																					                                                                																																																																																																				                                                                																																																																																																			                                                                																																																																																																		                                                                																																																																																																	                                                                																																																																																																                                                                																																																																																															                                                                																																																																																														                                                                																																																																																													                                                                																																																																																												                                                                																																																																																											                                                                																																																																																										                                                                																																																																																									                                                                																																																																																								                                                                																																																																																							                                                                																																																																																						                                                                																																																																																					                                                                																																																																																				                                                                																																																																																			                                                                																																																																																		                                                                																																																																																	                                                                																																																																																                                                                																																																																															                                                                																																																																														                                                                																																																																													                                                                																																																																												                                                                																																																																											                                                                																																																																										                                                                																																																																									                                                                																																																																								                                                                																																																																							                                                                																																																																						                                                                																																																																					                                                                																																																																				                                                                																																																																			                                                                																																																																		                                                                																																																																	                                                                 <?php selected('refunded', $status); ?>>Refunded</option>
																																																																																																																				                                        <option value="revoked"																																																																																																																				                                                               																																																																																																																			                                                               																																																																																																																		                                                               																																																																																																																	                                                               																																																																																																																                                                               																																																																																																															                                                               																																																																																																														                                                               																																																																																																													                                                               																																																																																																												                                                               																																																																																																											                                                               																																																																																																										                                                               																																																																																																									                                                               																																																																																																								                                                               																																																																																																							                                                               																																																																																																						                                                               																																																																																																					                                                               																																																																																																				                                                               																																																																																																			                                                               																																																																																																		                                                               																																																																																																	                                                               																																																																																																                                                               																																																																																															                                                               																																																																																														                                                               																																																																																													                                                               																																																																																												                                                               																																																																																											                                                               																																																																																										                                                               																																																																																									                                                               																																																																																								                                                               																																																																																							                                                               																																																																																						                                                               																																																																																					                                                               																																																																																				                                                               																																																																																			                                                               																																																																																		                                                               																																																																																	                                                               																																																																																                                                               																																																																															                                                               																																																																														                                                               																																																																													                                                               																																																																												                                                               																																																																											                                                               																																																																										                                                               																																																																									                                                               																																																																								                                                               																																																																							                                                               																																																																						                                                               																																																																					                                                               																																																																				                                                               																																																																			                                                               																																																																		                                                               																																																																	                                                                <?php selected('revoked', $status); ?>>Revoked</option>
																																																																																																																												    </select>
																																																																																																																				                                    <span class="chama-payment-status edit-status"><span class="chama-payment-status-icon																																																																																																																				                                                                                                                         																																																																																																																			                                                                                                                         																																																																																																																		                                                                                                                         																																																																																																																	                                                                                                                         																																																																																																																                                                                                                                         																																																																																																															                                                                                                                         																																																																																																														                                                                                                                         																																																																																																													                                                                                                                         																																																																																																												                                                                                                                         																																																																																																											                                                                                                                         																																																																																																										                                                                                                                         																																																																																																									                                                                                                                         																																																																																																								                                                                                                                         																																																																																																							                                                                                                                         																																																																																																						                                                                                                                         																																																																																																					                                                                                                                         																																																																																																				                                                                                                                         																																																																																																			                                                                                                                         																																																																																																		                                                                                                                         																																																																																																	                                                                                                                         																																																																																																                                                                                                                         																																																																																															                                                                                                                         																																																																																														                                                                                                                         																																																																																													                                                                                                                         																																																																																												                                                                                                                         																																																																																											                                                                                                                         																																																																																										                                                                                                                         																																																																																									                                                                                                                         																																																																																								                                                                                                                         																																																																																							                                                                                                                         																																																																																						                                                                                                                         																																																																																					                                                                                                                         																																																																																				                                                                                                                         																																																																																			                                                                                                                         																																																																																		                                                                                                                         																																																																																	                                                                                                                         																																																																																                                                                                                                         																																																																															                                                                                                                         																																																																														                                                                                                                         																																																																													                                                                                                                         																																																																												                                                                                                                         																																																																											                                                                                                                         																																																																										                                                                                                                         																																																																									                                                                                                                         																																																																								                                                                                                                         																																																																							                                                                                                                         																																																																						                                                                                                                         																																																																					                                                                                                                         																																																																				                                                                                                                         																																																																			                                                                                                                         																																																																		                                                                                                                         																																																																	                                                                                                                          <?php echo esc_attr($status); ?>"></span></span>
																																																																																																																				                                    <br/><br/>
																																																																																																																				                                </div>

																																																																																																																				<div class="chama-admin-box">

																																																																																																																													<div id="major-publishing-actions">
																																																																																																																														<div id="publishing-action">
																																																																																																																															<input type="submit" class="button button-primary right" value="Save Fund">
																																																																																																														                                                                    <?php if ($status === 'complete'): ?>
																																																																																																																															<a href="<?php echo esc_url($resend_receipt_url); ?>"  class="button-secondary right chama-resend-receipt">Resend Receipt</a>
																																																																																																											                                                                                <?php else: ?>
                                                                                    <a href="<?php echo esc_url($sync_payment_url); ?>"  class="button-secondary right chama-sync-payment">Sync Fund</a>
			                                                                    <?php endif; ?>									</div>
																<div class="clear"></div>
															</div>

														</div>
								<?php
                                    }
                                    endif;
                                    //stripe metabox
                                    add_action('admin_init', 'chama_fund_stripe_meta_metabox');
                                    if (! function_exists('chama_fund_stripe_meta_metabox')):
                                        function chama_fund_stripe_meta_metabox()
                                    {
                                            add_meta_box(
                                                'stripe_meta',
                                                __('Stripe Meta', 'chama'),
                                                'chama_fund_stripe_meta_display_metabox',
                                                'chama_fund',
                                                "side",
                                                "default"
                                            );

                                        }
                                    endif;
                                    //Displaying the meta box
                                    if (! function_exists('chama_fund_stripe_meta_display_metabox')):
                                        function chama_fund_stripe_meta_display_metabox($post)
                                    {
                                            $stripe_customer_id       = get_post_meta($post->ID, 'chama_stripe_customer_id', true);
                                            $stripe_payment_intent_id = get_post_meta($post->ID, 'chama_stripe_payment_intent_id', true);
                                        ?>
																																																																																																																				<p>
																																																																																																																				<strong>Transaction ID:</strong><br>
																																																																																																																				<?php echo esc_html($stripe_payment_intent_id); ?>
																																																																																																																				</p>
																																																																																																																				<p>
																																																																																																																				<strong>Customer ID:</strong><br>
																																																																																																																				<?php echo esc_html($stripe_customer_id); ?>
																																																																																																																				</p>
																																																																																																																						<?php
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            endif;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //This function saves the data you put in the meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            if (! function_exists('chama_fund_save_postdata')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                function chama_fund_save_postdata($post_id, $post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    if ((isset($_POST['chama_publish_nonce'])) && $post->post_type == 'chama_fund') {

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        //Not save if the user hasn't submitted changes
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        // Verifying whether input is coming from the proper form
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        // check
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if (! wp_verify_nonce(sanitize_key($_POST['chama_publish_nonce']), 'publish_metadata')) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        // Making sure the user has permission

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if (! current_user_can('edit_post', $post_id)) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if (! empty($_POST['chama_fund_total_amount'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            /* Get the posted data and sanitize it for use as an HTML class. */
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $data = str_replace(',', '', number_format(floatval($_POST['chama_fund_total_amount']), 2));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            update_post_meta($post_id, 'chama_fund_total_amount', $data);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            update_post_meta($post_id, 'chama_fund_total_amount', 0);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if (! empty($_POST['chama_payment_status'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            /* Get the posted data and sanitize it for use as an HTML class. */
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $data = sanitize_text_field(wp_unslash($_POST['chama_payment_status']));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            update_post_meta($post_id, 'chama_payment_status', $data);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            update_post_meta($post_id, 'chama_payment_status', 'processing');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_action('save_post_chama_fund', 'chama_fund_save_postdata', 20, 2);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //show columns
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_filter('manage_chama_fund_posts_columns', 'chama_fund_columns');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_fund_columns($columns)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                unset($columns['date']);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns = [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'cb'           => $columns['cb'],
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'funder'       => __('Funder', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'campaign'     => __('Campaign', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'status'       => __('Status', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'payment_date' => __('Date', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'amount'       => __('Amount', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'details'      => __('Details', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                return $columns;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_filter('manage_chama_fund_posts_custom_column', 'chama_populate_fund_columns', 10, 2);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_populate_fund_columns($column, $id)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                switch ($column) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    case 'amount':

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $currency = get_post_meta($id, 'chama_fund_currency', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $amount   = get_post_meta($id, 'chama_fund_total_amount', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        echo esc_html(CHAMA_SYMBOL . number_format((float) $amount, 2, '.', ''));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    case 'payment_date':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $fund_date           = get_post_meta($id, 'chama_fund_date_time', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $fund_date_formatted = gmdate("F, j, Y", strtotime($fund_date));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        echo esc_html($fund_date_formatted);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    case 'details':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $edit_link = get_edit_post_link($id);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        echo '<a href="' . esc_url($edit_link) . '" class="button button-small hint--top hint--bounce"><span class="dashicons dashicons-visibility"></span></a>';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    case 'status':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $test_mode        = get_post_meta($id, 'chama_is_test_fund', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $test_mode_notice = $test_mode ? '<span  class="chama-test-mode-label label-listing" aria-label="This fund was made in test mode." rel="tooltip">Test</span>' : '';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $status           = get_post_meta($id, 'chama_payment_status', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        echo '<span class="chama-payment-status listing-status"><span class="chama-payment-status-icon ' . esc_attr($status) . '"></span>&nbsp;' . esc_html(ucwords($status)) . '</span> ' . wp_kses($test_mode_notice, chama_allowed_html());
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    case 'funder':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $user_id        = get_post_meta($id, 'chama_user_id', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $user           = $user_id ? get_user_by('id', $user_id) : false;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $user_full_name = $user && $user->first_name && $user->last_name ? $user->first_name . ' ' . $user->last_name : false;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $user_email     = $user ? $user->user_email : '';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $display_name   = $user_full_name ? $user_full_name : $user_email;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $user_link      = admin_url('user-edit.php?user_id=') . $user_id;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        echo '<a href="' . esc_url($user_link) . '">' . esc_html($user_id) . ' - ' . esc_html($display_name) . '</a>';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    case 'campaign':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $campaign_id        = get_post_meta($id, 'chama_fund_item_id', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $description        = get_post_meta($id, 'chama_fund_description', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $funds_link         = admin_url('edit.php?post_type=chama_fund');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $chama_filter_nonce = wp_create_nonce('chama_filter_nonce');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $funds_link         = esc_url(add_query_arg(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                'cid' => $campaign_id,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                'cfn' => $chama_filter_nonce,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ],
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $funds_link
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        echo '<a href="' . esc_url($funds_link) . '">' . esc_html($description) . '</a>';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    default:
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //Extend search for funds(description)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_action('admin_init', 'chama_fund_extend_search');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_fund_extend_search()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                global $typenow;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if ($typenow === 'chama_fund') {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    add_filter('posts_search', 'chama_fund_search_override', 10, 2);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_fund_search_override($search, $query)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                global $wpdb;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if ($query->is_main_query() && ! empty($query->query['s'])) {

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $like   = '%' . $wpdb->esc_like($query->query['s']) . '%';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $search = preg_replace("#\({$wpdb->posts}.post_title LIKE [^)]+\)\K#",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $wpdb->prepare("
or exists (
select * from {$wpdb->postmeta} where post_id={$wpdb->posts}.ID
and meta_key in ('chama_user_id', 'chama_email', 'chama_full_name' , 'chama_fund_description')
and meta_value like %s
)
", $like), $search);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                return $search;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //Filter funds
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_action('parse_query', 'chama_filter_funds');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_filter_funds($query)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                global $pagenow;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if (! is_admin() || 'chama_fund' !== $query->query['post_type'] || 'edit.php' !== $pagenow) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $nonce = '';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if (isset($_GET['cfn'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $nonce = sanitize_text_field(wp_unslash($_GET['cfn']));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if (! $nonce || ! wp_verify_nonce($nonce, 'chama_filter_nonce')) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $meta_query_args = null;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $user_id     = isset($_GET['uid']) ? sanitize_text_field(wp_unslash($_GET['uid'])) : '';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $campaign_id = isset($_GET['cid']) ? sanitize_text_field(wp_unslash($_GET['cid'])) : '';

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if (! $user_id && ! $campaign_id) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if ($user_id) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $meta_query_args = [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'key'   => 'chama_user_id',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'value' => $user_id,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if ($campaign_id) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $meta_query_args = [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'key'   => 'chama_fund_item_id',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'value' => $campaign_id,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if ($query->is_main_query() && $meta_query_args) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $query->set('meta_query', [$meta_query_args]);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            /*=============================================COMMISSION ORDER=============================================*/
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            /**
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             * Add Commission Order metaboxes
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             */
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_action('admin_init', 'chama_com_order_information_metabox');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            if (! function_exists('chama_com_order_information_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                function chama_com_order_information_metabox()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    add_meta_box(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'commission_order_information',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        __('Commission Order Information', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'chama_com_order_information_display_metabox',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'chama_com_order',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "normal",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "high"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    );

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //Displaying the meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            if (! function_exists('chama_com_order_information_display_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                function chama_com_order_information_display_metabox($post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $commission_order_date           = get_post_meta($post->ID, 'chama_com_order_date_time', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $commission_order_date_formatted = gmdate("F, j, Y H:i:s", strtotime($commission_order_date));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $commission_order_total          = get_post_meta($post->ID, 'chama_com_order_total_amount', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $commission_order_total_display  = CHAMA_SYMBOL . number_format((float) $commission_order_total, 2, '.', '');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $commission_title                = get_post_meta($post->ID, 'chama_com_order_description', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $commission_request              = get_post_meta($post->ID, 'chama_com_order_request', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $commission_price                = get_post_meta($post->ID, 'chama_com_order_price', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $commission_shipping             = get_post_meta($post->ID, 'chama_com_order_shipping', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $commission_addons               = get_post_meta($post->ID, 'chama_com_order_addons', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $commission_shipping_address     = get_post_meta($post->ID, 'chama_com_order_shipping_address', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $commission_price_cents          = $commission_price * CHAMA_CURRENCY_MINIMUM;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $commission_display_price        = chama_get_formatted_price($commission_price_cents);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    if ($commission_shipping) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $shipping              = explode('|', $commission_shipping);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $shipping_cost         = $shipping[1];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $shipping_location     = $shipping[0];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $shipping_cost_cents   = $shipping_cost * CHAMA_CURRENCY_MINIMUM;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $shipping_display_cost = chama_get_formatted_price($shipping_cost_cents);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $commission_addons_html           = '';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $commission_shipping_address_html = '';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $allowed_tags                     = [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'strong' => [],
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'br'     => [],
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'em'     => [],
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    if ($commission_addons) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $addons = explode(',', $commission_addons);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        foreach ($addons as $addon) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $item                   = explode('|', $addon);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $item_amount            = $item[1];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $item_amount_cents      = $item_amount * CHAMA_CURRENCY_MINIMUM;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $item_display_amount    = chama_get_formatted_price($item_amount_cents);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $commission_addons_html = $commission_addons_html . '<em>' . esc_html($item[0]) . '</em>:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . esc_html($item_display_amount) . "<br/>";
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    if ($commission_shipping_address) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        foreach ($commission_shipping_address as $line_item) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $commission_shipping_address_html = $commission_shipping_address_html . '<em>' . esc_html($line_item['commission_shipping_address_key']) . '</em>:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . esc_html($line_item['commission_shipping_address_value']) . "<br/>";
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ?>
																																																																																																																				        <h2 class="chama-metabox-heading">
																																																																																																																				        Commission Order Information				</h2>
																																																																																																																						<div class="chama-metabox-row">
																																																																																																																						<div class="chama-metabox-col">
																																																																																																																						<p>
																																																																																																																																		<strong>Transaction Date:</strong><br>
																																																																																																																																		<?php echo esc_html($commission_order_date_formatted); ?>										</p>
																																																																																																																						</div>
																																																																																																				                                                                        <div class="chama-metabox-col">
																																																																																																																						<p>
																																																																																																																																		<strong>Base Price:</strong><br>
																																																																																																																																		<?php echo esc_html($commission_display_price); ?>												</p>
																																																																																																																						</div>
																																																																																																																						<div class="chama-metabox-col">
																																																																																																																						<p>
																																																																																																																																		<strong>Total Amount:</strong><br>
																																																																																																																																		<?php echo esc_html($commission_order_total_display); ?>												</p>
																																																																																																																						</div>
																																																																																																											                                            <div class="chama-metabox-col">
																																																																																																																						<p>
																																																																																																																																		<strong>Commission:</strong><br>
																																																																																																																																		<?php echo esc_html($commission_title); ?>												</p>

																																																																																																																						</div>
																																																																																																																						</div>
																																																																																																				                                                                        <hr />
																																																																																																				                                                                        <div class="chama-metabox-row">
																																																																																																				                                                                        <div class="chama-metabox-col">
																																																																																																				                                                                        <strong>Add ons:</strong><br>
																																																																																																				                                                                        <?php echo wp_kses($commission_addons_html, $allowed_tags); ?>
																																																																																																				                                                                        </div>
																																																																												                                                                                                                                                                        <?php if ($commission_shipping): ?>
																																																																																																				                                                                        <div class="chama-metabox-col">
																																																																																																				                                                                        <strong>Shipping Fee:</strong><br>
																																																																																																				                                                                        Ship to																																																																																																				                                                                               																																																																																																			                                                                               																																																																																																		                                                                               																																																																																																	                                                                               																																																																																																                                                                               																																																																																															                                                                               																																																																																														                                                                               																																																																																													                                                                               																																																																																												                                                                               																																																																																											                                                                               																																																																																										                                                                               																																																																																									                                                                               																																																																																								                                                                               																																																																																							                                                                               																																																																																						                                                                               																																																																																					                                                                               																																																																																				                                                                               																																																																																			                                                                               																																																																																		                                                                               																																																																																	                                                                               																																																																																                                                                               																																																																															                                                                               																																																																														                                                                               																																																																													                                                                               																																																																												                                                                               																																																																											                                                                               																																																																										                                                                               																																																																									                                                                               																																																																								                                                                               																																																																							                                                                               																																																																						                                                                               																																																																					                                                                               																																																																				                                                                               																																																																			                                                                               																																																																		                                                                               																																																																	                                                                               																																																																                                                                               																																																															                                                                               																																																														                                                                               																																																													                                                                               																																																												                                                                               																																																											                                                                               																																																										                                                                               																																																									                                                                               																																																								                                                                               																																																							                                                                               																																																						                                                                               																																																					                                                                               																																																				                                                                               																																																			                                                                               																																																		                                                                               																																																	                                                                                <?php echo esc_html($shipping_location); ?><br>
																																																																																																				                                                                        <?php echo esc_html($shipping_display_cost); ?><br>
																																																																																																				                                                                        </div>
																																																																																																		                                                                                <div class="chama-metabox-col">
																																																																																																				                                                                        <strong>Shipping Address:</strong><br>
																																																																																																		                                                                                <?php echo wp_kses($commission_shipping_address_html, $allowed_tags); ?>
																																																																																																				                                                                        </div>
																																																																												                                                                                                                                                                        <?php endif; ?>
																								                                                                        </div>
																								                                                                        <hr />
																								                                                                        <div class="chama-metabox-row">
																								                                                                        <div class="chama-metabox-col">
																								                                                                        <strong>Request:</strong><br>
																								                                                                        <?php echo esc_html($commission_request); ?>
																								                                                                        </div>
																								                                                                        </div>

																																										<?php
                                                                                                                                                                            }
                                                                                                                                                                            endif;

                                                                                                                                                                            add_action('admin_init', 'chama_commission_commissioner_details_metabox');
                                                                                                                                                                            if (! function_exists('chama_commission_commissioner_details_metabox')):
                                                                                                                                                                                function chama_commission_commissioner_details_metabox()
                                                                                                                                                                            {
                                                                                                                                                                                    add_meta_box(
                                                                                                                                                                                        'commission_commissioner_details',
                                                                                                                                                                                        __('Commissioner Details', 'chama'),
                                                                                                                                                                                        'chama_commission_commissioner_details_display_metabox',
                                                                                                                                                                                        'chama_com_order',
                                                                                                                                                                                        "normal",
                                                                                                                                                                                        "high"
                                                                                                                                                                                    );

                                                                                                                                                                                }
                                                                                                                                                                            endif;
                                                                                                                                                                            //Displaying the meta box
                                                                                                                                                                            if (! function_exists('chama_commission_commissioner_details_display_metabox')):
                                                                                                                                                                                function chama_commission_commissioner_details_display_metabox($post)
                                                                                                                                                                            {

                                                                                                                                                                                    $user_id                = get_post_meta($post->ID, 'chama_user_id', true);
                                                                                                                                                                                    $user                   = $user_id ? get_user_by('id', $user_id) : false;
                                                                                                                                                                                    $user_full_name         = $user ? $user->first_name . ' ' . $user->last_name : '';
                                                                                                                                                                                    $user_email             = $user ? $user->user_email : '';
                                                                                                                                                                                    $commission_orders_link = admin_url('edit.php?post_type=chama_com_order');
                                                                                                                                                                                    $user_link              = admin_url('user-edit.php?user_id=') . $user_id;
                                                                                                                                                                                    // Create account disconnect URL
                                                                                                                                                                                    $commission_orders_link = esc_url(add_query_arg(
                                                                                                                                                                                        [
                                                                                                                                                                                            'uid' => $user_id,
                                                                                                                                                                                        ],
                                                                                                                                                                                        $commission_orders_link
                                                                                                                                                                                    ));
                                                                                                                                                                                ?>
																																																																																																																				         <h2 class="chama-metabox-heading">
																																																																																																																				         Commissioner Details				</h2>
																																																																																																																						<div class="chama-metabox-row">
																																																																																																																						<div class="chama-metabox-col">
																																																																																																																						<p>
																																																																																																																																		<strong>Commissioner ID:</strong><br>
																																																																																																																																		<?php echo esc_html($user_id); ?><br/>
																																																																																																														                                                                                <a href="<?php echo esc_url($user_link) ?>">User profile</a><br/>
																																																																																																															                                                                            <a href="<?php echo esc_url($commission_orders_link) ?>">All orders for this commissioner</a>										</p>
																																																																																																																						</div>
																																																																																																																						<div class="chama-metabox-col">
																																																																																																																						<p>
																																																																																																																																		<strong>Commissioner Name:</strong><br>
																																																																																																																																		<?php echo esc_html($user_full_name); ?>												</p>
																																																																																																																						</div>
																																																																																																																				        <div class="chama-metabox-col">
																																																																																																																						<p>
																																																																																																																																		<strong>Commissioner Email:</strong><br>
																																																																																																																																		<?php echo esc_html($user_email); ?>												</p>

																																																																																																																						</div>
																																																																																																																						</div>
																																																																																																																						<?php
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //publish metabox
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_action('admin_init', 'chama_com_order_publish_metabox');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            if (! function_exists('chama_com_order_publish_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                function chama_com_order_publish_metabox()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    add_meta_box(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'submitdiv',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        __('Update Commission Order', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'chama_com_order_publish_display_metabox',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'chama_com_order',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "side",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "default"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    );

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //Displaying the meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            if (! function_exists('chama_com_order_publish_display_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                function chama_com_order_publish_display_metabox($post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $delete_url         = get_delete_post_link($post->ID, '', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $status             = get_post_meta($post->ID, 'chama_payment_status', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $current_url        = home_url(chama_get_current_request_uri());
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $resend_receipt_url = esc_url(add_query_arg(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'id'           => (int) $post->ID,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'chama_nonce'  => wp_create_nonce('chama_resend_receipt_nonce'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'chama_action' => 'resend-receipt',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'type'         => 'chama_commission',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ],
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $current_url
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $sync_payment_url = esc_url(add_query_arg(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'id'           => (int) $post->ID,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'chama_nonce'  => wp_create_nonce('chama_sync_payment_nonce'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'chama_action' => 'sync-payment',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ],
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $current_url
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ));

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    add_query_arg('account-updated', 'true', $current_url);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ?>
																																																																																																																				        <div class="chama-publish-header">
																																																																																																																													<h3 class="hndle">Update Commission Order</h3>

																																																																																																																													<span class="delete-payment"><a title="Delete Commission Order" class="delete-single-payment delete-payment-button dashicons dashicons-trash" href="<?php echo esc_url($delete_url) ?>" aria-label="Delete Commission Order"></a></span>
																																																																																																																				                                </div>
																																																																																																																				                                <div class="chama-publish-box-inside ">

																																																																																																																				                                <?php
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        wp_nonce_field('publish_metadata', 'chama_publish_nonce');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            chama_price_display_metabox($post->ID, 'chama_com_order_total_amount', '', 'Total Amount');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ?>
																																																																																																																				        <div>
																																																																																																																				            <label><strong>Status:</strong></label>
																																																																																																																				        </div>
																																																																																																																				         <select name="chama_payment_status" id="chama_payment_status"  >

																																																																																																																				                                        <option value="abandoned"																																																																																																																				                                                                 																																																																																																																			                                                                 																																																																																																																		                                                                 																																																																																																																	                                                                 																																																																																																																                                                                 																																																																																																															                                                                 																																																																																																														                                                                 																																																																																																													                                                                 																																																																																																												                                                                 																																																																																																											                                                                 																																																																																																										                                                                 																																																																																																									                                                                 																																																																																																								                                                                 																																																																																																							                                                                 																																																																																																						                                                                 																																																																																																					                                                                 																																																																																																				                                                                 																																																																																																			                                                                 																																																																																																		                                                                 																																																																																																	                                                                 																																																																																																                                                                 																																																																																															                                                                 																																																																																														                                                                 																																																																																													                                                                 																																																																																												                                                                 																																																																																											                                                                 																																																																																										                                                                 																																																																																									                                                                 																																																																																								                                                                 																																																																																							                                                                 																																																																																						                                                                 																																																																																					                                                                 																																																																																				                                                                 																																																																																			                                                                 																																																																																		                                                                 																																																																																	                                                                 																																																																																                                                                 																																																																															                                                                 																																																																														                                                                 																																																																													                                                                 																																																																												                                                                 																																																																											                                                                 																																																																										                                                                 																																																																									                                                                 																																																																								                                                                 																																																																							                                                                 																																																																						                                                                 																																																																					                                                                 																																																																				                                                                 																																																																			                                                                 																																																																		                                                                 																																																																	                                                                  <?php selected('abandoned', $status); ?>>Abandoned</option>
																																																																																																																				                                        <option value=" canceled"																																																																																																																				                                                                 																																																																																																																			                                                                 																																																																																																																		                                                                 																																																																																																																	                                                                 																																																																																																																                                                                 																																																																																																															                                                                 																																																																																																														                                                                 																																																																																																													                                                                 																																																																																																												                                                                 																																																																																																											                                                                 																																																																																																										                                                                 																																																																																																									                                                                 																																																																																																								                                                                 																																																																																																							                                                                 																																																																																																						                                                                 																																																																																																					                                                                 																																																																																																				                                                                 																																																																																																			                                                                 																																																																																																		                                                                 																																																																																																	                                                                 																																																																																																                                                                 																																																																																															                                                                 																																																																																														                                                                 																																																																																													                                                                 																																																																																												                                                                 																																																																																											                                                                 																																																																																										                                                                 																																																																																									                                                                 																																																																																								                                                                 																																																																																							                                                                 																																																																																						                                                                 																																																																																					                                                                 																																																																																				                                                                 																																																																																			                                                                 																																																																																		                                                                 																																																																																	                                                                 																																																																																                                                                 																																																																															                                                                 																																																																														                                                                 																																																																													                                                                 																																																																												                                                                 																																																																											                                                                 																																																																										                                                                 																																																																									                                                                 																																																																								                                                                 																																																																							                                                                 																																																																						                                                                 																																																																					                                                                 																																																																				                                                                 																																																																			                                                                 																																																																		                                                                 																																																																	                                                                  <?php selected(' canceled', $status); ?>> Canceled</option>
																																																																																																																				                                        <option value="complete"																																																																																																																				                                                                																																																																																																																			                                                                																																																																																																																		                                                                																																																																																																																	                                                                																																																																																																																                                                                																																																																																																															                                                                																																																																																																														                                                                																																																																																																													                                                                																																																																																																												                                                                																																																																																																											                                                                																																																																																																										                                                                																																																																																																									                                                                																																																																																																								                                                                																																																																																																							                                                                																																																																																																						                                                                																																																																																																					                                                                																																																																																																				                                                                																																																																																																			                                                                																																																																																																		                                                                																																																																																																	                                                                																																																																																																                                                                																																																																																															                                                                																																																																																														                                                                																																																																																													                                                                																																																																																												                                                                																																																																																											                                                                																																																																																										                                                                																																																																																									                                                                																																																																																								                                                                																																																																																							                                                                																																																																																						                                                                																																																																																					                                                                																																																																																				                                                                																																																																																			                                                                																																																																																		                                                                																																																																																	                                                                																																																																																                                                                																																																																															                                                                																																																																														                                                                																																																																													                                                                																																																																												                                                                																																																																											                                                                																																																																										                                                                																																																																									                                                                																																																																								                                                                																																																																							                                                                																																																																						                                                                																																																																					                                                                																																																																				                                                                																																																																			                                                                																																																																		                                                                																																																																	                                                                 <?php selected('complete', $status); ?>>Complete</option>
																																																																																																																				                                        <option value="failed"																																																																																																																				                                                              																																																																																																																			                                                              																																																																																																																		                                                              																																																																																																																	                                                              																																																																																																																                                                              																																																																																																															                                                              																																																																																																														                                                              																																																																																																													                                                              																																																																																																												                                                              																																																																																																											                                                              																																																																																																										                                                              																																																																																																									                                                              																																																																																																								                                                              																																																																																																							                                                              																																																																																																						                                                              																																																																																																					                                                              																																																																																																				                                                              																																																																																																			                                                              																																																																																																		                                                              																																																																																																	                                                              																																																																																																                                                              																																																																																															                                                              																																																																																														                                                              																																																																																													                                                              																																																																																												                                                              																																																																																											                                                              																																																																																										                                                              																																																																																									                                                              																																																																																								                                                              																																																																																							                                                              																																																																																						                                                              																																																																																					                                                              																																																																																				                                                              																																																																																			                                                              																																																																																		                                                              																																																																																	                                                              																																																																																                                                              																																																																															                                                              																																																																														                                                              																																																																													                                                              																																																																												                                                              																																																																											                                                              																																																																										                                                              																																																																									                                                              																																																																								                                                              																																																																							                                                              																																																																						                                                              																																																																					                                                              																																																																				                                                              																																																																			                                                              																																																																		                                                              																																																																	                                                               <?php selected('failed', $status); ?>>Failed</option>
																																																																																																																												        <option value="processing"																																																																																																																												                                  																																																																																																																											                                  																																																																																																																										                                  																																																																																																																									                                  																																																																																																																								                                  																																																																																																																							                                  																																																																																																																						                                  																																																																																																																					                                  																																																																																																																				                                  																																																																																																																			                                  																																																																																																																		                                  																																																																																																																	                                  																																																																																																																                                  																																																																																																															                                  																																																																																																														                                  																																																																																																													                                  																																																																																																												                                  																																																																																																											                                  																																																																																																										                                  																																																																																																									                                  																																																																																																								                                  																																																																																																							                                  																																																																																																						                                  																																																																																																					                                  																																																																																																				                                  																																																																																																			                                  																																																																																																		                                  																																																																																																	                                  																																																																																																                                  																																																																																															                                  																																																																																														                                  																																																																																													                                  																																																																																												                                  																																																																																											                                  																																																																																										                                  																																																																																									                                  																																																																																								                                  																																																																																							                                  																																																																																						                                  																																																																																					                                  																																																																																				                                  																																																																																			                                  																																																																																		                                  																																																																																	                                  																																																																																                                  																																																																															                                  																																																																														                                  																																																																													                                  																																																																												                                  																																																																											                                  																																																																										                                  																																																																									                                   <?php selected('processing', $status); ?>>Processing</option>
																																																																																																																				                                        <option value="refunded"																																																																																																																				                                                                																																																																																																																			                                                                																																																																																																																		                                                                																																																																																																																	                                                                																																																																																																																                                                                																																																																																																															                                                                																																																																																																														                                                                																																																																																																													                                                                																																																																																																												                                                                																																																																																																											                                                                																																																																																																										                                                                																																																																																																									                                                                																																																																																																								                                                                																																																																																																							                                                                																																																																																																						                                                                																																																																																																					                                                                																																																																																																				                                                                																																																																																																			                                                                																																																																																																		                                                                																																																																																																	                                                                																																																																																																                                                                																																																																																															                                                                																																																																																														                                                                																																																																																													                                                                																																																																																												                                                                																																																																																											                                                                																																																																																										                                                                																																																																																									                                                                																																																																																								                                                                																																																																																							                                                                																																																																																						                                                                																																																																																					                                                                																																																																																				                                                                																																																																																			                                                                																																																																																		                                                                																																																																																	                                                                																																																																																                                                                																																																																															                                                                																																																																														                                                                																																																																													                                                                																																																																												                                                                																																																																											                                                                																																																																										                                                                																																																																									                                                                																																																																								                                                                																																																																							                                                                																																																																						                                                                																																																																					                                                                																																																																				                                                                																																																																			                                                                																																																																		                                                                																																																																	                                                                 <?php selected('refunded', $status); ?>>Refunded</option>
																																																																																																																				                                        <option value="revoked"																																																																																																																				                                                               																																																																																																																			                                                               																																																																																																																		                                                               																																																																																																																	                                                               																																																																																																																                                                               																																																																																																															                                                               																																																																																																														                                                               																																																																																																													                                                               																																																																																																												                                                               																																																																																																											                                                               																																																																																																										                                                               																																																																																																									                                                               																																																																																																								                                                               																																																																																																							                                                               																																																																																																						                                                               																																																																																																					                                                               																																																																																																				                                                               																																																																																																			                                                               																																																																																																		                                                               																																																																																																	                                                               																																																																																																                                                               																																																																																															                                                               																																																																																														                                                               																																																																																													                                                               																																																																																												                                                               																																																																																											                                                               																																																																																										                                                               																																																																																									                                                               																																																																																								                                                               																																																																																							                                                               																																																																																						                                                               																																																																																					                                                               																																																																																				                                                               																																																																																			                                                               																																																																																		                                                               																																																																																	                                                               																																																																																                                                               																																																																															                                                               																																																																														                                                               																																																																													                                                               																																																																												                                                               																																																																											                                                               																																																																										                                                               																																																																									                                                               																																																																								                                                               																																																																							                                                               																																																																						                                                               																																																																					                                                               																																																																				                                                               																																																																			                                                               																																																																		                                                               																																																																	                                                                <?php selected('revoked', $status); ?>>Revoked</option>
																																																																																																																												    </select>
																																																																																																																				                                    <span class="chama-payment-status edit-status"><span class="chama-payment-status-icon																																																																																																																				                                                                                                                         																																																																																																																			                                                                                                                         																																																																																																																		                                                                                                                         																																																																																																																	                                                                                                                         																																																																																																																                                                                                                                         																																																																																																															                                                                                                                         																																																																																																														                                                                                                                         																																																																																																													                                                                                                                         																																																																																																												                                                                                                                         																																																																																																											                                                                                                                         																																																																																																										                                                                                                                         																																																																																																									                                                                                                                         																																																																																																								                                                                                                                         																																																																																																							                                                                                                                         																																																																																																						                                                                                                                         																																																																																																					                                                                                                                         																																																																																																				                                                                                                                         																																																																																																			                                                                                                                         																																																																																																		                                                                                                                         																																																																																																	                                                                                                                         																																																																																																                                                                                                                         																																																																																															                                                                                                                         																																																																																														                                                                                                                         																																																																																													                                                                                                                         																																																																																												                                                                                                                         																																																																																											                                                                                                                         																																																																																										                                                                                                                         																																																																																									                                                                                                                         																																																																																								                                                                                                                         																																																																																							                                                                                                                         																																																																																						                                                                                                                         																																																																																					                                                                                                                         																																																																																				                                                                                                                         																																																																																			                                                                                                                         																																																																																		                                                                                                                         																																																																																	                                                                                                                         																																																																																                                                                                                                         																																																																															                                                                                                                         																																																																														                                                                                                                         																																																																													                                                                                                                         																																																																												                                                                                                                         																																																																											                                                                                                                         																																																																										                                                                                                                         																																																																									                                                                                                                         																																																																								                                                                                                                         																																																																							                                                                                                                         																																																																						                                                                                                                         																																																																					                                                                                                                         																																																																				                                                                                                                         																																																																			                                                                                                                         																																																																		                                                                                                                         																																																																	                                                                                                                          <?php echo esc_attr($status); ?>"></span></span>
																																																																																																																				                                    <br/><br/>
																																																																																																																				                                </div>

																																																																																																																				<div class="chama-admin-box">

																																																																																																																													<div id="major-publishing-actions">
																																																																																																																														<div id="publishing-action">
																																																																																																																															<input type="submit" class="button button-primary right" value="Save Order">
																																																																																																														                                                                    <?php if ($status === 'complete'): ?>
																																																																																																																															<a href="<?php echo esc_url($resend_receipt_url); ?>"  class="button-secondary right chama-resend-receipt">Resend Receipt</a>
																																																																																																											                                                                                <?php else: ?>
                                                                                    <a href="<?php echo esc_url($sync_payment_url); ?>"  class="button-secondary right chama-sync-payment">Sync Order</a>
			                                                                    <?php endif; ?>									</div>
																<div class="clear"></div>
															</div>

														</div>
								<?php
                                    }
                                    endif;
                                    //stripe metabox
                                    add_action('admin_init', 'chama_com_order_stripe_meta_metabox');
                                    if (! function_exists('chama_com_order_stripe_meta_metabox')):
                                        function chama_com_order_stripe_meta_metabox()
                                    {
                                            add_meta_box(
                                                'stripe_meta',
                                                __('Stripe Meta', 'chama'),
                                                'chama_com_order_stripe_meta_display_metabox',
                                                'chama_com_order',
                                                "side",
                                                "default"
                                            );

                                        }
                                    endif;
                                    //Displaying the meta box
                                    if (! function_exists('chama_com_order_stripe_meta_display_metabox')):
                                        function chama_com_order_stripe_meta_display_metabox($post)
                                    {
                                            $stripe_customer_id       = get_post_meta($post->ID, 'chama_stripe_customer_id', true);
                                            $stripe_payment_intent_id = get_post_meta($post->ID, 'chama_stripe_payment_intent_id', true);
                                        ?>
																																																																																																																				<p>
																																																																																																																				<strong>Transaction ID:</strong><br>
																																																																																																																				<?php echo esc_html($stripe_payment_intent_id); ?>
																																																																																																																				</p>
																																																																																																																				<p>
																																																																																																																				<strong>Customer ID:</strong><br>
																																																																																																																				<?php echo esc_html($stripe_customer_id); ?>
																																																																																																																				</p>
																																																																																																																						<?php
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            endif;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //This function saves the data you put in the meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            if (! function_exists('chama_com_order_save_postdata')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                function chama_com_order_save_postdata($post_id, $post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    if ((isset($_POST['chama_publish_nonce'])) && $post->post_type == 'chama_com_order') {

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        //Not save if the user hasn't submitted changes
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        // Verifying whether input is coming from the proper form
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        // check
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if (! wp_verify_nonce(sanitize_key($_POST['chama_publish_nonce']), 'publish_metadata')) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        // Making sure the user has permission

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if (! current_user_can('edit_post', $post_id)) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if (! empty($_POST['chama_com_order_total_amount'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            /* Get the posted data and sanitize it for use as an HTML class. */
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $data = str_replace(',', '', number_format(floatval($_POST['chama_com_order_total_amount']), 2));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            update_post_meta($post_id, 'chama_com_order_total_amount', $data);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            update_post_meta($post_id, 'chama_com_order_total_amount', 0);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if (! empty($_POST['chama_payment_status'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            /* Get the posted data and sanitize it for use as an HTML class. */
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $data = sanitize_text_field(wp_unslash($_POST['chama_payment_status']));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            update_post_meta($post_id, 'chama_payment_status', $data);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            update_post_meta($post_id, 'chama_payment_status', 'processing');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            endif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_action('save_post_chama_com_order', 'chama_com_order_save_postdata', 20, 2);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //show columns
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_filter('manage_chama_com_order_posts_columns', 'chama_com_order_columns');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_com_order_columns($columns)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                unset($columns['date']);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns = [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'cb'           => $columns['cb'],
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'commissioner' => __('Commissioner', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'commission'   => __('Commission', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'status'       => __('Status', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'payment_date' => __('Date', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'amount'       => __('Amount', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'details'      => __('Details', 'chama'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                return $columns;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_filter('manage_chama_com_order_posts_custom_column', 'chama_populate_commission_order_columns', 10, 2);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_populate_commission_order_columns($column, $id)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                switch ($column) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    case 'amount':

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $currency = get_post_meta($id, 'chama_com_order_currency', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $amount   = get_post_meta($id, 'chama_com_order_total_amount', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        echo esc_html(CHAMA_SYMBOL . number_format((float) $amount, 2, '.', ''));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    case 'payment_date':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $commission_order_date           = get_post_meta($id, 'chama_com_order_date_time', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $commission_order_date_formatted = gmdate("F, j, Y", strtotime($commission_order_date));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        echo esc_html($commission_order_date_formatted);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    case 'details':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $edit_link = get_edit_post_link($id);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        echo '<a href="' . esc_url($edit_link) . '" class="button button-small hint--top hint--bounce"><span class="dashicons dashicons-visibility"></span></a>';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    case 'status':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $test_mode        = get_post_meta($id, 'chama_is_test_commission_order', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $test_mode_notice = $test_mode ? '<span  class="chama-test-mode-label label-listing" aria-label="This commission_order was made in test mode." rel="tooltip">Test</span>' : '';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $status           = get_post_meta($id, 'chama_payment_status', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        echo '<span class="chama-payment-status listing-status"><span class="chama-payment-status-icon ' . esc_attr($status) . '"></span>&nbsp;' . esc_html(ucwords($status)) . '</span> ' . wp_kses($test_mode_notice, chama_allowed_html());
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    case 'commissioner':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $user_id        = get_post_meta($id, 'chama_user_id', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $user           = $user_id ? get_user_by('id', $user_id) : false;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $user_full_name = $user && $user->first_name && $user->last_name ? $user->first_name . ' ' . $user->last_name : false;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $user_email     = $user ? $user->user_email : '';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $display_name   = $user_full_name ? $user_full_name : $user_email;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $user_link      = admin_url('user-edit.php?user_id=') . $user_id;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        echo '<a href="' . esc_url($user_link) . '">' . esc_html($user_id) . ' - ' . esc_html($display_name) . '</a>';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    case 'commission':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $commission_id          = get_post_meta($id, 'chama_com_order_item_id', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $description            = get_post_meta($id, 'chama_com_order_description', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $commission_orders_link = admin_url('edit.php?post_type=chama_com_order');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $chama_filter_nonce     = wp_create_nonce('chama_filter_nonce');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $commission_orders_link = esc_url(add_query_arg(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                'cid' => $commission_id,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                'cfn' => $chama_filter_nonce,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ],
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $commission_orders_link
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        echo '<a href="' . esc_url($commission_orders_link) . '">' . esc_html($description) . '</a>';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    default:
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //Extend search for commission_orders(description)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_action('admin_init', 'chama_com_order_extend_search');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_com_order_extend_search()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                global $typenow;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if ($typenow === 'chama_com_order') {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    add_filter('posts_search', 'chama_com_order_search_override', 10, 2);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_com_order_search_override($search, $query)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                global $wpdb;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if ($query->is_main_query() && ! empty($query->query['s'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $like   = '%' . $wpdb->esc_like($query->query['s']) . '%';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $search = preg_replace("#\({$wpdb->posts}.post_title LIKE [^)]+\)\K#",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $wpdb->prepare("
or exists (
select * from {$wpdb->postmeta} where post_id={$wpdb->posts}.ID
and meta_key in ('chama_user_id', 'chama_email', 'chama_full_name' , 'chama_com_order_description')
and meta_value like %s
)
", $like), $search);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                return $search;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //Filter commission_orders
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_action('parse_query', 'chama_filter_commission_orders');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_filter_commission_orders($query)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                global $pagenow;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if (
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ! is_admin() ||
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'edit.php' !== $pagenow ||
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ! isset($query->query['post_type']) ||
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'chama_com_order' !== $query->query['post_type']
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $nonce = '';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if (isset($_GET['cfn'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $nonce = sanitize_text_field(wp_unslash($_GET['cfn']));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $user_id       = isset($_GET['uid']) ? sanitize_text_field(wp_unslash($_GET['uid'])) : '';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $commission_id = isset($_GET['cid']) ? sanitize_text_field(wp_unslash($_GET['cid'])) : '';

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $nonce_is_valid = $nonce && wp_verify_nonce($nonce, 'chama_filter_nonce');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $has_filters    = $user_id || $commission_id;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if (! $nonce_is_valid || ! $has_filters) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $meta_query_args = [];

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if ($user_id) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $meta_query_args[] = [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'key'   => 'chama_user_id',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'value' => $user_id,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if ($commission_id) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $meta_query_args[] = [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'key'   => 'chama_com_order_item_id',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'value' => $commission_id,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if ($query->is_main_query() && ! empty($meta_query_args)) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $query->set('meta_query', $meta_query_args);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            /*=============================================SYNC PAYMENT=============================================*/
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //sync payment
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_action('admin_init', 'chama_sync_payment');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_sync_payment()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if (
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    isset($_GET['chama_action']) &&
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $_GET['chama_action'] === 'sync-payment' &&
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    isset($_GET['chama_nonce']) &&
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    wp_verify_nonce(sanitize_key($_GET['chama_nonce']), 'chama_sync_payment_nonce')
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $payment_id = isset($_GET['id']) ? sanitize_text_field(wp_unslash($_GET['id'])) : '';

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    if (! $payment_id) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $cpt                      = get_post_type($payment_id);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $stripe_payment_intent_id = get_post_meta($payment_id, 'chama_stripe_payment_intent_id', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $user_id                  = get_post_meta($payment_id, 'chama_user_id', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $secret_key               = chama_is_live_mode() ? CHAMA_STRIPE_LIVE_SECRET_KEY : CHAMA_STRIPE_TEST_SECRET_KEY;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    if ($secret_key && $stripe_payment_intent_id) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        try {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $stripe         = new \ChamaWP\Vendor\Stripe\StripeClient($secret_key);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $payment_intent = $stripe->paymentIntents->retrieve($stripe_payment_intent_id);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            if ($cpt && $user_id && $payment_intent) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                chama_process_incomplete_payment($payment_id, $cpt, $payment_intent, $user_id, true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        } catch (\Exception $e) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //error_log('Stripe API error: ' . $e->getMessage());
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            /*=============================================SYNC SUBSCRIPTION=============================================*/
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //sync subscription
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_action('admin_init', 'chama_sync_subscription');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_sync_subscription()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                // Only allow if correct action and valid nonce
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if (
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    isset($_GET['chama_action'], $_GET['chama_nonce']) &&
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $_GET['chama_action'] === 'sync-subscription' &&
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    wp_verify_nonce(sanitize_key($_GET['chama_nonce']), 'chama_sync_subscription_nonce')
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    // Optional: restrict to admins
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    if (! current_user_can('manage_options')) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $subscription_id = isset($_GET["id"]) ? sanitize_text_field(wp_unslash($_GET["id"])) : '';

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    if (! $subscription_id) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $cpt = get_post_type($subscription_id);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    if (! $cpt || ! post_type_exists($cpt)) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $stripe_customer_id     = get_post_meta($subscription_id, 'chama_stripe_customer_id', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $stripe_subscription_id = get_post_meta($subscription_id, 'chama_stripe_subscription_id', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $tier_id                = get_post_meta($subscription_id, 'chama_subscription_item_id', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $tier_description       = get_post_meta($subscription_id, 'chama_subscription_description', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $subscription_level     = get_post_meta($subscription_id, 'chama_subscription_total_amount', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $user_id                = get_post_meta($subscription_id, 'chama_user_id', true);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    if (! $stripe_subscription_id) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        // Mark as canceled if missing
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        update_post_meta($subscription_id, 'chama_subscription_status', 'canceled');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        chama_update_subscription_transactions($stripe_subscription_id, 'canceled');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        add_action('admin_notices', function () {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            echo '<div class="notice-error notice"><p>';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            esc_html_e('Could not obtain the subscription details for this transaction from Stripe.', 'chama');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            echo '</p></div>';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        });
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $secret_key = chama_is_live_mode() ? CHAMA_STRIPE_LIVE_SECRET_KEY : CHAMA_STRIPE_TEST_SECRET_KEY;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    if (! $secret_key) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    try {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $stripe       = new \ChamaWP\Vendor\Stripe\StripeClient($secret_key);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $subscription = $stripe->subscriptions->retrieve($stripe_subscription_id, []);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if ($subscription_id && $cpt && $user_id && $subscription) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            chama_process_incomplete_subscription(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $subscription_id,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $cpt,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $stripe_customer_id,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $stripe_subscription_id,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $tier_id,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $tier_description,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $subscription_level,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $subscription,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $user_id,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                true
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            );
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    } catch (\Exception $e) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        //error_log('Stripe sync error: ' . $e->getMessage());
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        add_action('admin_notices', function () use ($e) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            echo '<div class="notice-error notice"><p>';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            echo esc_html__('Stripe error: ', 'chama') . esc_html($e->getMessage());
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            echo '</p></div>';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        });
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            /*=============================================RESEND RECEIPT=============================================*/
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            // resend confirmation
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_action('admin_init', 'chama_resend_receipt');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_resend_receipt()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if (
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    isset($_GET['chama_action'], $_GET['chama_nonce']) &&
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $_GET['chama_action'] === 'resend-receipt' &&
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    wp_verify_nonce(sanitize_key(wp_unslash($_GET['chama_nonce'])), 'chama_resend_receipt_nonce')
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    if (isset($_GET['id'], $_GET['type'])) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $receipt_id        = sanitize_text_field(wp_unslash($_GET["id"]));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $type              = sanitize_text_field(wp_unslash($_GET["type"]));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $from              = sprintf("%s <%s>", CHAMA_FROM_NAME, CHAMA_FROM_EMAIL);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $from_header       = 'From: ' . $from;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $headers           = ['Content-Type: text/html; charset=UTF-8', $from_header];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $supporter_message = $notification = $total = $total_amount_formatted = $description = $cpt = $commission_request = $commission_price = $commission_display_price = $commission_shipping = $commission_addons = $commission_addons_html = $shipping_location = $shipping_display_cost = '';

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if (get_option('chama_supporter_enable_payment_notification')) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $payment    = get_post($receipt_id);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $user_id    = get_post_meta($receipt_id, 'chama_user_id', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $user       = $user_id ? get_user_by('id', $user_id) : false;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $user_email = $user ? $user->user_email : '';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            switch ($type) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                case 'one-time':

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $cpt = 'chama_donation';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    if ($payment) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $total                  = get_post_meta($receipt_id, 'chama_donation_total_amount', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $total_amount_formatted = CHAMA_SYMBOL . number_format((float) $total, 2, '.', '');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $notification           = 'The donation receipt has been resent.';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $subject = 'Donation Receipt';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                case 'membership':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $cpt = 'chama_subscription';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    if ($payment) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $total                  = get_post_meta($receipt_id, 'chama_subscription_total_amount', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $total_amount_formatted = CHAMA_SYMBOL . number_format((float) $total, 2, '.', '');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $description            = get_post_meta($receipt_id, 'chama_subscription_description', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $notification           = 'The subscription receipt has been resent.';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $subject = 'Subscription Receipt';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                case 'crowdfunding':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $cpt = 'chama_fund';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    if ($payment) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $total                  = get_post_meta($receipt_id, 'chama_fund_total_amount', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $total_amount_formatted = CHAMA_SYMBOL . number_format((float) $total, 2, '.', '');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $description            = get_post_meta($receipt_id, 'chama_fund_description', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $notification           = 'The fund receipt has been resent.';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $subject = 'Crowdfunding Receipt';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                case 'commission':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $cpt = 'chama_com_order';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    if ($payment) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $total                    = get_post_meta($receipt_id, 'chama_com_order_total_amount', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $total_amount_formatted   = CHAMA_SYMBOL . number_format((float) $total, 2, '.', '');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $description              = get_post_meta($receipt_id, 'chama_com_order_description', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $commission_request       = get_post_meta($receipt_id, 'chama_com_order_request', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $commission_price         = get_post_meta($receipt_id, 'chama_com_order_price', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $commission_price_cents   = $commission_price * CHAMA_CURRENCY_MINIMUM;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $commission_display_price = chama_get_formatted_price($commission_price_cents);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $commission_shipping      = get_post_meta($receipt_id, 'chama_com_order_shipping', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $commission_addons        = get_post_meta($receipt_id, 'chama_com_order_addons', true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $notification             = 'The order receipt has been resent.';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $subject                  = 'Commission Receipt';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                default:
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            if (get_option($cpt . '_receipt_template')) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $supporter_message = get_option($cpt . '_receipt_template');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $supporter_message = wpautop($supporter_message);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                switch ($cpt) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    case 'chama_donation':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        //replace donation email template variables
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        foreach ([
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            "{date}"       => gmdate('F j, Y'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            "{amount}"     => $total_amount_formatted,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            "{receipt_id}" => $receipt_id,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ] as $k => $v) {$supporter_message = str_replace($k, $v, $supporter_message);}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    case 'chama_subscription':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        //replace subscription email template variables
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $today = gmdate('Y-m-d');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        foreach ([
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            "{description}"  => $description,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            "{date}"         => gmdate('F j, Y'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            "{renewal-date}" => gmdate("F, j, Y", strtotime('+1 month', strtotime($today))),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            "{amount}"       => $total_amount_formatted,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            "{receipt_id}"   => $receipt_id,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ] as $k => $v) {$supporter_message = str_replace($k, $v, $supporter_message);}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    case 'chama_fund':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        //replace fund email template variables
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        foreach ([
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            "{description}" => $description,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            "{date}"        => gmdate('F j, Y'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            "{amount}"      => $total_amount_formatted,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            "{receipt_id}"  => $receipt_id,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ] as $k => $v) {$supporter_message = str_replace($k, $v, $supporter_message);}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    case 'chama_com_order':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $shipping              = explode('|', $commission_shipping);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $shipping_cost         = $shipping[1];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $shipping_location     = $shipping[0];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $shipping_cost_cents   = $shipping_cost * CHAMA_CURRENCY_MINIMUM;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $shipping_display_cost = chama_get_formatted_price($shipping_cost_cents);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if ($commission_addons) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $addons = explode(',', $commission_addons);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            foreach ($addons as $addon) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $item                   = explode('|', $addon);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $item_amount            = $item[1];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $item_amount_cents      = $item_amount * CHAMA_CURRENCY_MINIMUM;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $item_display_amount    = chama_get_formatted_price($item_amount_cents);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $commission_addons_html = $commission_addons_html . '<strong>' . esc_html($item[0]) . '</strong>: ' . esc_html($item_display_amount) . "<br/>";
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        //replace commission order email template variables
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        foreach ([
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            "{description}"       => $description,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            "{price}"             => $commission_display_price,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            "{date}"              => gmdate('F j, Y'),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            "{request}"           => $commission_request,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            "{add_ons}"           => $commission_addons_html,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            "{shipping-location}" => $shipping_location,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            "{shipping-fee}"      => $shipping_display_cost,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            "{amount}"            => $total_amount_formatted,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            "{receipt_id}"        => $receipt_id,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ] as $k => $v) {$supporter_message = str_replace($k, $v, $supporter_message);}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    default:
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                chama_send_email($user_email, $subject, $supporter_message, $headers);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                add_action('admin_notices', function () use ($notification) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    echo '<div class="notice-success notice"><p>', esc_html($notification), '</p></div>';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                });
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            /*=============================================USERS=============================================*/

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //search users

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_action('pre_user_query', 'chama_extend_user_search');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_extend_user_search($u_query)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                // make sure that this code will be applied only for user search
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if ($u_query->query_vars['search']) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    global $wpdb;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $search_query = trim($u_query->query_vars['search'], '*');

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    // Adding first name to the search
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $u_query->query_from .= " JOIN {$wpdb->usermeta} fname ON fname.user_id = {$wpdb->users}.ID AND fname.meta_key = 'first_name'";

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    // Adding last name to the search
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $u_query->query_from .= " JOIN {$wpdb->usermeta} lname ON lname.user_id = {$wpdb->users}.ID AND lname.meta_key = 'last_name'";

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $search_by = ['user_login', 'user_email', 'fname.meta_value', 'lname.meta_value'];

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $u_query->query_where = $u_query->get_search_sql($search_query, $search_by, 'both');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            /*=============================================SORTING=============================================*/
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            /**
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             * Sorting columns
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             */

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_filter('manage_edit-tier_sortable_columns', 'chama_tier_sortable_columns');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_tier_sortable_columns($columns)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['limit']       = 'member_limit';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['price']       = 'tier_price';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['highlighted'] = 'tier_is_highlighted';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                return $columns;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_filter('manage_edit-campaign_sortable_columns', 'chama_campaign_sortable_columns');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_campaign_sortable_columns($columns)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['target']      = 'campaign_target_amount';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['start']       = 'campaign_starting_amount';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['min']         = 'campaign_min_donation';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['show_target'] = 'campaign_show_target_amount';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['end_date']    = 'campaign_end_date';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                return $columns;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_filter('manage_edit-commission_sortable_columns', 'chama_commission_sortable_columns');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_commission_sortable_columns($columns)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['price']        = 'commission_price';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['flexible']     = 'commission_pay_what_you_want';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['quantity']     = 'commission_limit';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['availability'] = 'commission_can_request';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                return $columns;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_filter('manage_edit-donation_sortable_columns', 'chama_donation_sortable_columns');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_donation_sortable_columns($columns)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['donor']        = 'chama_user_id';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['amount']       = 'chama_donation_total_amount';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['status']       = 'chama_payment_status';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['payment_date'] = 'chama_donation_date_time';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                return $columns;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_filter('manage_edit-subscription_sortable_columns', 'chama_subscription_sortable_columns');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_subscription_sortable_columns($columns)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['member']              = 'chama_user_id';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['tier']                = 'chama_subscription_description';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['amount']              = 'chama_subscription_total_amount';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['status']              = 'chama_payment_status';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['subscription_status'] = 'chama_subscription_status';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['transaction']         = 'chama_subscription_event';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['payment_date']        = 'chama_subscription_date_time';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['renewal_date']        = 'chama_subscription_renewal_date';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                return $columns;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_filter('manage_edit-fund_sortable_columns', 'chama_fund_sortable_columns');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_fund_sortable_columns($columns)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['funder']       = 'chama_user_id';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['campaign']     = 'chama_fund_description';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['amount']       = 'chama_fund_total_amount';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['status']       = 'chama_payment_status';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['payment_date'] = 'chama_fund_date_time';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                return $columns;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_filter('manage_edit-commission_order_sortable_columns', 'chama_com_order_sortable_columns');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_com_order_sortable_columns($columns)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['commissioner'] = 'chama_user_id';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['commission']   = 'chama_com_order_description';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['amount']       = 'chama_com_order_total_amount';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['status']       = 'chama_payment_status';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $columns['payment_date'] = 'chama_com_order_date_time';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                return $columns;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_action('pre_get_posts', 'chama_posts_orderby');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_posts_orderby($query)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if (! is_admin() || ! $query->is_main_query()) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $col             = $query->get('orderby');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $numeric_columns = ['chama_user_id', 'tier_price', 'member_limit', 'campaign_target_amount', 'campaign_starting_amount', 'campaign_min_donation', 'commission_price', 'commission_limit', 'chama_donation_total_amount', 'chama_subscription_total_amount', 'chama_fund_total_amount', 'chama_com_order_total_amount'];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $yes_no_columns  = ['tier_is_highlighted', 'campaign_show_target_amount', 'commission_pay_what_you_want', 'commission_can_request'];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $date_columns    = ['campaign_end_date', 'chama_donation_date_time', 'chama_subscription_renewal_date'];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $text_columns    = ['chama_payment_status', 'chama_subscription_event', 'chama_subscription_status'];

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if (in_array($col, $numeric_columns)) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $query->set('orderby', 'meta_value');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $query->set('meta_key', $col);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $query->set('meta_type', 'numeric');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if (in_array($col, $yes_no_columns)) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $meta_query = [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'relation'             => 'OR',

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        // Clause 1, named my_column_exists:
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        // Query posts that do have the metadata _my_column.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'my_column_exists'     => [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'key'     => $col, // meta key
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'compare' => 'EXISTS',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ],

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        // Clause 2, named my_column_not_exists:
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        // OR that do NOT have the metadata.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        'my_column_not_exists' => [
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'key'     => $col, // meta key
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'compare' => 'NOT EXISTS',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ],
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ];

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $query->set('meta_query', $meta_query);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if (in_array($col, $date_columns) || in_array($col, $text_columns)) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $query->set('meta_key', $col);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $query->set('orderby', 'meta_value');

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //custom row actions
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            add_filter('post_row_actions', 'chama_custom_action_row', 10, 2);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            function chama_custom_action_row($actions, $post)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                switch ($post->post_type) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    case 'chama_donation':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        unset($actions['edit']);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        unset($actions['inline hide-if-no-js']);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        unset($actions['trash']);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        unset($actions['view']);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $actions['trash'] = "<a class='submitdelete' title='" . esc_attr(__('Delete this donation permanently', 'chama')) . "' href='" . get_delete_post_link($post->ID, '', true) . "'>" . __('Delete', 'chama') . "</a>";
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    default:
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                return $actions;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            /*=============================================HELPERS=============================================*/
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            /**
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             * Metabox helpers
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             */

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            //Displaying number meta box
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            if (! function_exists('chama_number_display_metabox')):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                function chama_number_display_metabox($id, $key, $desc)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $content = get_post_meta($id, $key, true);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ?>
<?php if ($desc): ?>
																																																																																																																																		            <p>
																																																																																																																																					                        <?php echo esc_html($desc); ?>
																																																																																																																																					                        </p>
																																																																																																																																		            <?php endif; ?>

									<p>
									<input class="widefat" type="number" name="<?php echo esc_attr($key); ?>" id="<?php echo esc_attr($key); ?>"
									    value="<?php echo esc_attr(empty($content) ? 0 : $content); ?>" size="30" />
									</p>
									<?php
                                        }
                                        endif;
                                        //Displaying price meta box
                                        if (! function_exists('chama_price_display_metabox')):
                                            function chama_price_display_metabox($id, $key, $desc, $label = '', $min = 0, $info = '')
                                        {
                                                $content = get_post_meta($id, $key, true);
                                            ?>
<?php if ($desc): ?>
																																																																																																																																		            <p>
																																																																																																																																					                        <?php echo esc_html($desc); ?>
																																																																																																																																					                        </p>
																																																				                                                                                                                                                                                                                                                                                                                                                            <?php if ($info): ?>
																																																			                                                                                                                                                                                                                                                                                                                                                                    <h4 style="color: #2271b1;">
																																																				                                        <?php echo esc_html($info); ?>
																																																				                                        </h4>
																																																				                                        </p>
																																																				                                    <?php endif?><?php endif; ?>
									<p>
                                    <?php if ($label): ?>
                                        <div>
                                        <label><strong>
																	                        <?php echo esc_html($label); ?>
																	                        </strong></label>
                                        </div>

														            <?php endif; ?>
			            <div class="chama-input-wrapper">
		                <div class="chama-input-prepend">
		                                    <span class="chama-input-prepend-text">

		                        <?php echo esc_html(CHAMA_SYMBOL); ?>            </span>
		                                </div>
		                <input class="widefat chama-input" type="number" min="<?php echo esc_attr($min); ?>" step="any" name="<?php echo esc_attr($key); ?>" id="<?php echo esc_attr($key); ?>"
		                value="<?php echo esc_attr(empty($content) ? 0 : $content); ?>" size="30" />
		                </div>

									</p>
									<?php
                                        }
                                        endif;
                                        //Displaying date metabox
                                        if (! function_exists('chama_date_display_metabox')):
                                            function chama_date_display_metabox($id, $key, $desc, $label = '')
                                        {
                                                $content         = get_post_meta($id, $key, true);
                                                $formatted_date  = new DateTime($content);
                                                $dateWithoutTime = $formatted_date->format('Y-m-d');
                                            ?>
<?php if ($desc): ?>
																																																																																																																																		            <p>
																																																																																																																																					                        <?php echo esc_html($desc); ?>
																																																																																																																																					                        </p>
																																																																																																																																		            <?php endif; ?>
            <p>
            <?php if ($label): ?>
                                        <label for="<?php echo esc_attr($key); ?>"><strong><?php echo esc_html($label); ?></strong></label>&nbsp;
                                    <?php endif; ?>
                                    <input class="widefat chama-input" type="date" name="<?php echo esc_attr($key); ?>" id="<?php echo esc_attr($key); ?>"
                                    value="<?php echo esc_attr($dateWithoutTime); ?>" size="30" />
									</p>




									<?php
                                        }
                                        endif;
                                        if (! function_exists('chama_checkbox_display_metabox')):
                                            function chama_checkbox_display_metabox($id, $key, $desc, $label)
                                        {
                                                $content = get_post_meta($id, $key, true);
                                            ?>
<?php if ($desc): ?>
																																																																																																																																		            <p>
																																																																																																																																					                        <?php echo esc_html($desc); ?>
																																																																																																																																					                        </p>
																																																																																																																																		            <?php endif; ?>
                    <p>
    <input type="checkbox" name="<?php echo esc_attr($key); ?>"<?php if ($content == "yes"): echo esc_attr(" checked");endif?> value="yes"><?php echo esc_html($label); ?>
    </p>
    <?php
        }
        endif;
        if (! function_exists('chama_textarea_display_metabox')):
            function chama_textarea_display_metabox($id, $key, $desc, $max)
        {
                $content = get_post_meta($id, $key, true);
            ?>
<?php if ($desc): ?>
																																																																																																																																		            <p>
																																																																																																																																					                        <?php echo esc_html($desc); ?>
																																																																																																																																					                        </p>
																																																																																																																																		            <?php endif; ?>
												<p>
												<textarea class="widefat"  name="<?php echo esc_attr($key); ?>" id="<?php echo esc_attr($key); ?>" maxlength="<?php echo esc_attr($max); ?>" cols="40" rows="4" style="min-height: 200px;"><?php echo esc_html("$content"); ?></textarea>
												</p>
												<?php
                                                    }
                                                    endif;
                                                    //Displaying number meta box
                                                    if (! function_exists('chama_youtube_url_display_metabox')):
                                                        function chama_youtube_url_display_metabox($id, $key, $desc)
                                                    {
                                                            $content = get_post_meta($id, $key, true);
                                                        ?>
<?php if ($desc): ?>
																																																																																																																																		            <p>
																																																																																																																																					                        <?php echo esc_html($desc); ?>
																																																																																																																																					                        </p>
																																																																																																																																		            <?php endif; ?>

									<p>
                                    <div class="chama-input-wrapper">
		                <div class="chama-input-prepend">
		                                    <span class="chama-input-prepend-text">https://www.youtube.com/embed/</span>
		                                </div>
		                <input class="widefat chama-input" type="text" name="<?php echo esc_attr($key); ?>" id="<?php echo esc_attr($key); ?>"
		                value="<?php echo esc_html("$content"); ?>" size="30" />
		                </div>
									</p>
									<?php
                                        }
                                        endif;
                                        /**
                                         * Select dropdown
                                         */
                                        if (! function_exists('chama_select_generator')):
                                            function chama_select_generator(array $args)
                                        {
                                                $html               = '';
                                                $name               = $args['name'];
                                                $value              = $args['value'] ? $args['value'] : [];
                                                $select_options     = $args['select_options'];
                                                $disabled_label     = $args['disabled_label'];
                                                $selected_attribute = $value == '-1' ? 'selected="selected"' : '';

                                                $html .= '<select name="' . esc_attr($name) . '" id="' . esc_attr($name) . '" class="widefat">';
                                                if ($disabled_label) {
                                                    $html .= '<option disabled="disabled" ' . $selected_attribute . ' value="">' . $disabled_label . '</option>';
                                                }
                                                foreach ($select_options as $k => $v) {
                                                    $selected = false;
                                                    if ($k == $value) {
                                                        $selected = true;
                                                    }
                                                    $html .= '<option ' . selected($selected, true, false) . ' value="' . esc_attr($k) . '">' . $v . '</option>';
                                                }
                                                $html .= '</select> ';

                                                echo wp_kses($html, chama_allowed_html());
                                            }

                                        endif;
                                        add_filter('screen_options_show_screen', 'chama_remove_screen_options');
                                        function chama_remove_screen_options($args)
                                        {

                                            global $post;

                                            /* Check against the current post object */
                                            $chama_post_types = ['chama_tier', 'chama_commission', 'chama_campaign', 'chama_donation', 'chama_fund', 'chama_com_order', 'chama_subscription'];
                                            if ($post && in_array($post->post_type, $chama_post_types)) {
                                                return false;
                                            }

                                            return $args;

                                        }
                                        add_action('edit_user_profile', 'chama_user_links');

                                        function chama_user_links($user)
                                        {
                                            $user_id = absint(filter_input(INPUT_GET, 'user_id', FILTER_SANITIZE_NUMBER_INT));
                                            if (! is_admin() && ! $user_id) {
                                                return;
                                            }

                                            $donations_link         = admin_url('edit.php?post_type=chama_donation');
                                            $subscription_link      = admin_url('edit.php?post_type=chama_subscription');
                                            $commission_orders_link = admin_url('edit.php?post_type=chama_com_order');
                                            $funds_link             = admin_url('edit.php?post_type=chama_fund');
                                            $chama_filter_nonce     = wp_create_nonce('chama_filter_nonce');
                                            $query_args             = [
                                                'uid' => $user_id,
                                                'cfn' => $chama_filter_nonce,
                                            ];
                                            $stripe_customer_id = $stripe_subscription_id = $tier_id = $tier_description = $subscription_level = $stripe_current_period_start = $stripe_current_period_end = $subscription_status = '';
                                            if ($user_id) {
                                                $stripe_customer_id          = get_user_meta($user_id, 'chama_stripe_customer_id', true);
                                                $stripe_subscription_id      = get_user_meta($user_id, 'chama_stripe_subscription_id', true);
                                                $subscription_status         = get_user_meta($user_id, 'chama_subscription_status', true);
                                                $tier_id                     = get_user_meta($user_id, 'chama_tier_id', true);
                                                $tier_description            = get_user_meta($user_id, 'chama_tier_description', true);
                                                $subscription_level          = get_user_meta($user_id, 'chama_subscription_level', true);
                                                $subscription_level_display  = CHAMA_SYMBOL . number_format((float) $subscription_level, 2, '.', '');
                                                $stripe_current_period_start = get_user_meta($user_id, 'chama_subscription_current_period_start', true);
                                                $stripe_current_period_end   = get_user_meta($user_id, 'chama_subscription_current_period_end', true);
                                                $payment_method_id           = get_user_meta($user_id, 'chama_payment_method_id', true);
                                                $payment_method_brand        = get_user_meta($user_id, 'chama_payment_method_brand', true);
                                                $payment_method_expiration   = get_user_meta($user_id, 'chama_payment_method_expiration', true);
                                                $payment_method_last4        = get_user_meta($user_id, 'chama_payment_method_last4', true);
                                            }
                                            // Create account disconnect URL
                                            $donations_link = esc_url(add_query_arg(
                                                $query_args,
                                                $donations_link
                                            ));
                                            $subscription_link = esc_url(add_query_arg(
                                                $query_args,
                                                $subscription_link
                                            ));
                                            $commission_orders_link = esc_url(add_query_arg(
                                                $query_args,
                                                $commission_orders_link
                                            ));
                                            $funds_link = esc_url(add_query_arg(
                                                $query_args,
                                                $funds_link
                                            ));
                                        ?>
<p>
<div class="chama-card">

<h3>ChamaWP - Information</h3>
<ul class="chama-list">
    <li><a href="<?php echo esc_url($donations_link) ?>">View donations for this user</a>	</li>
    <li><a href="<?php echo esc_url($subscription_link) ?>">View subscriptions for this user</a>	</li>
    <li><a href="<?php echo esc_url($funds_link) ?>">View crowdfunding donations for this user</a>	</li>
    <li><a href="<?php echo esc_url($commission_orders_link) ?>">View commission orders for this user</a>	</li>
</ul>
<?php if ($stripe_subscription_id && $tier_id && $tier_description && $subscription_level && $stripe_current_period_start && $stripe_current_period_end): ?>
    <hr/>
    <h3>Subscription Information</h3>
    <table class="chama-info-table">
        <tr>
        <th>Customer ID</th><td><?php echo esc_html($stripe_customer_id); ?></td>
        </tr>
        <tr>
        <th>Subscription ID</th><td><?php echo esc_html($stripe_subscription_id); ?></td>
        </tr>
        <tr>
        <th>Subscription Status</th><td><?php echo esc_html(ucfirst($subscription_status)); ?> <span class="chama-subscription-status-icon<?php echo esc_attr($subscription_status); ?>"></span></td>
        </tr>
        <tr>
        <th>Tier</th><td><?php echo esc_html($tier_description); ?></td>
        </tr>
        <tr>
        <th>Monthly payment</th><td><?php echo esc_html($subscription_level_display); ?></td>
        </tr>
        <tr>
        <th>Current period start</th><td><?php echo esc_html($stripe_current_period_start); ?></td>
        </tr>
        <tr>
        <th>Current period end</th><td><?php echo esc_html($stripe_current_period_end); ?></td>
    </tr>
    <tr>
        <th>Payment ID</th><td><?php echo esc_html($payment_method_id); ?></td>
    </tr>
    <tr>
        <th>Credit Card</th><td><?php echo esc_html($payment_method_brand); ?><br/><?php echo esc_html($payment_method_expiration); ?><br/><?php echo esc_html($payment_method_last4); ?><br/></td>
    </tr>
    </table>

<?php endif; ?>
</div>
</p>

	<?php
        }
        function chama_flush_permalinks()
        {
            flush_rewrite_rules();
        }

        //add_action( 'wp_loaded', 'chama_flush_permalinks' );
        add_action('wp_loaded', function () {
            if (! is_admin()) { // Only target the front end
                chama_flush_permalinks();
            }
        });

  if (! function_exists('chama_send_newsletter_email_on_publish')):
    function chama_send_newsletter_email_on_publish($new_status, $old_status, $post)
{
        if ($post->post_type !== 'chama_newsletter') {
            return;
        }
        // Only run when transitioning to 'publish' from a non-published status
        if ($new_status === 'publish' && $old_status !== 'publish') {
            $post_id = $post->ID;
            if (! function_exists('chama_get_protection_data')
            || ! function_exists('chama_can_user_access_post')
            || ! function_exists('chama_send_email')) {
                return;
            }

            // Get protection data for the post
            $protection_data = chama_get_protection_data($post_id);
            $is_protected    = $protection_data['is_protected'];
            $post_level      = $protection_data['post_level'];

            if (! $is_protected) {
                $post_level = '';
            }

            $post = get_post($post_id);
            if (! $post) {
                return;
            }

            // Setup email headers
            $subject = $post->post_title;
            $message = apply_filters('the_content', $post->post_content);

            $from        = sprintf("%s <%s>", CHAMA_FROM_NAME, CHAMA_FROM_EMAIL);
            $from_header = 'From: ' . $from;
            $headers     = ['Content-Type: text/html; charset=UTF-8', $from_header];

            $args = [
                'meta_query' => [
                    'relation' => 'OR',
                    [
                        'key'     => 'chama_subscription_level',
                        'compare' => 'EXISTS',
                    ],
                    [
                        'key'     => 'is_email_verified',
                        'value'   => '1',
                        'compare' => '=',
                    ],
                ],
                'fields'     => ['ID', 'user_email'],
                'number'     => -1,
            ];
            $user_query = new WP_User_Query($args);
            $users      = $user_query->get_results();

            if (empty($users)) {
                return;
            }

            foreach ($users as $user) {
                // Get user ID
                $user_id = $user->ID;

                // Check if the user has opted in and is email verified
                $is_opted_in       = get_user_meta($user_id, 'newsletter_optin', true);
                $is_email_verified = get_user_meta($user_id, 'is_email_verified', true);

                // Determine if we should send the email based on opt-in and email verification
                $send_email = $is_opted_in && $is_email_verified;

                // Check if the user can access the post
                $can_access_post = chama_can_user_access_post($post_level, $user_id);

                // If the user is verified, opted in, and can access the post, send the email
                if ($can_access_post && $send_email) {
                    // Get the opt-out code for the user
                    $optout_code = get_user_meta($user_id, 'optout_code', true);
                    if ($optout_code) {
                        // Create the verification link
                        $unsubscribe_link = add_query_arg(
                            [
                                'uid'  => $user_id,
                                'code' => $optout_code, // Assuming you meant to use $optout_code here
                            ],
                            CHAMA_NEWSLETTER_OPT_OUT_CONFIRMATION_URL
                        );

                        // Append the unsubscribe message
                        $message .= '<br>---<br><small>You received this email because you signed up for our mailing list. To unsubscribe from this mailing list, <a href="' . esc_url($unsubscribe_link) . '">click here</a></small>.';
                    }
                    chama_send_email($user->user_email, $subject, $message, $headers);
                }
            }
        }
    }
endif;
add_action('transition_post_status', 'chama_send_newsletter_email_on_publish', 10, 3);
