/** * Render Give Form Columns * * @since 1.0 * * @param string $column_name Column name * @param int $post_id Give Form (Post) ID * * @return void */ function give_render_form_columns($column_name, $post_id) { if (get_post_type($post_id) == 'give_forms') { switch ($column_name) { case 'form_category': echo get_the_term_list($post_id, 'give_forms_category', '', ', ', ''); break; case 'form_tag': echo get_the_term_list($post_id, 'give_forms_tag', '', ', ', ''); break; case 'price': if (give_has_variable_prices($post_id)) { echo give_price_range($post_id); } else { echo give_price($post_id, false); echo '<input type="hidden" class="formprice-' . $post_id . '" value="' . give_get_form_price($post_id) . '" />'; } break; case 'goal': $goal_option = get_post_meta($post_id, '_give_goal_option', true); if (!empty($goal_option) && $goal_option === 'yes') { echo give_goal($post_id, false); } else { echo esc_html__('No Goal Set', 'give'); } echo '<input type="hidden" class="formgoal-' . $post_id . '" value="' . give_get_form_goal($post_id) . '" />'; break; case 'donations': if (current_user_can('view_give_form_stats', $post_id)) { echo '<a href="' . esc_url(admin_url('edit.php?post_type=give_forms&page=give-reports&tab=logs&view=sales&form=' . $post_id)) . '">'; echo give_get_form_sales_stats($post_id); echo '</a>'; } else { echo '-'; } break; case 'earnings': if (current_user_can('view_give_form_stats', $post_id)) { echo '<a href="' . esc_url(admin_url('edit.php?post_type=give_forms&page=give-reports&view=forms&form-id=' . $post_id)) . '">'; echo give_currency_filter(give_format_amount(give_get_form_earnings_stats($post_id))); echo '</a>'; } else { echo '-'; } break; case 'shortcode': echo '<input onclick="this.setSelectionRange(0, this.value.length)" type="text" class="shortcode-input" readonly="" value="[give_form id="' . absint($post_id) . '"]">'; break; } } }
/** * Render Give Form Columns * * @since 1.0 * * @param string $column_name Column name * @param int $post_id Give Form (Post) ID * * @return void */ function give_render_form_columns($column_name, $post_id) { if (get_post_type($post_id) == 'give_forms') { global $give_options; $style = isset($give_options['button_style']) ? $give_options['button_style'] : 'button'; $color = isset($give_options['checkout_color']) ? $give_options['checkout_color'] : 'blue'; $color = $color == 'inherit' ? '' : $color; $purchase_text = !empty($give_options['add_to_cart_text']) ? $give_options['add_to_cart_text'] : __('Purchase', 'give'); switch ($column_name) { case 'form_category': echo get_the_term_list($post_id, 'give_forms_category', '', ', ', ''); break; case 'form_tag': echo get_the_term_list($post_id, 'give_forms_tag', '', ', ', ''); break; case 'price': if (give_has_variable_prices($post_id)) { echo give_price_range($post_id); } else { echo give_price($post_id, false); echo '<input type="hidden" class="formprice-' . $post_id . '" value="' . give_get_form_price($post_id) . '" />'; } break; case 'goal': echo give_goal($post_id, false); echo '<input type="hidden" class="formgoal-' . $post_id . '" value="' . give_get_form_goal($post_id) . '" />'; break; case 'donations': if (current_user_can('view_give_forms_stats', $post_id)) { echo '<a href="' . esc_url(admin_url('edit.php?post_type=give_forms&page=give-reports&tab=logs&view=sales&form=' . $post_id)) . '">'; echo give_get_form_sales_stats($post_id); echo '</a>'; } else { echo '-'; } break; case 'earnings': if (current_user_can('view_give_forms_stats', $post_id)) { echo '<a href="' . esc_url(admin_url('edit.php?post_type=give_forms&page=give-reports&view=forms&form-id=' . $post_id)) . '">'; echo give_currency_filter(give_format_amount(give_get_form_earnings_stats($post_id))); echo '</a>'; } else { echo '-'; } break; case 'shortcode': echo '<input onclick="this.setSelectionRange(0, this.value.length)" type="text" class="shortcode-input" readonly="" value="[give_form id="' . absint($post_id) . '"]">'; break; } } }
/** * Generate PDF Reports. * * Generates PDF report on donations and income for all forms for the current year. * * @since 1.0 * * @param string $data * * @uses give_pdf */ function give_generate_pdf($data) { if (!current_user_can('view_give_reports')) { wp_die(esc_html__('You do not have permission to generate PDF sales reports.', 'give'), esc_html__('Error', 'give'), array('response' => 403)); } if (!wp_verify_nonce($_GET['_wpnonce'], 'give_generate_pdf')) { wp_die(esc_html__('Nonce verification failed.', 'give'), esc_html__('Error', 'give'), array('response' => 403)); } require_once GIVE_PLUGIN_DIR . '/includes/libraries/fpdf/fpdf.php'; require_once GIVE_PLUGIN_DIR . '/includes/libraries/fpdf/give_pdf.php'; $daterange = utf8_decode(sprintf(esc_html__('%1$s to %2$s', 'give'), date_i18n(get_option('date_format'), mktime(0, 0, 0, 1, 1, date('Y'))), date_i18n(get_option('date_format')))); $pdf = new give_pdf(); $pdf->AddPage('L', 'A4'); $pdf->SetTitle(utf8_decode(__('Donation report for the current year for all forms', 'give'))); $pdf->SetAuthor(utf8_decode(__('Give - Democratizing Generosity', 'give'))); $pdf->SetCreator(utf8_decode(__('Give - Democratizing Generosity', 'give'))); $pdf->Image(apply_filters('give_pdf_export_logo', GIVE_PLUGIN_URL . 'assets/images/give-logo-small.png'), 247, 8); $pdf->SetMargins(8, 8, 8); $pdf->SetX(8); $pdf->SetFont('Helvetica', '', 16); $pdf->SetTextColor(50, 50, 50); $pdf->Cell(0, 3, utf8_decode(__('Donation report for the current year for all forms', 'give')), 0, 2, 'L', false); $pdf->SetFont('Helvetica', '', 13); $pdf->Ln(); $pdf->SetTextColor(150, 150, 150); $pdf->Cell(0, 6, utf8_decode(__('Date Range: ', 'give')) . $daterange, 0, 2, 'L', false); $pdf->Ln(); $pdf->SetTextColor(50, 50, 50); $pdf->SetFont('Helvetica', '', 14); $pdf->Cell(0, 10, utf8_decode(__('Table View', 'give')), 0, 2, 'L', false); $pdf->SetFont('Helvetica', '', 12); $pdf->SetFillColor(238, 238, 238); $pdf->Cell(70, 6, utf8_decode(__('Form Name', 'give')), 1, 0, 'L', true); $pdf->Cell(30, 6, utf8_decode(__('Price', 'give')), 1, 0, 'L', true); $pdf->Cell(50, 6, utf8_decode(__('Categories', 'give')), 1, 0, 'L', true); $pdf->Cell(50, 6, utf8_decode(__('Tags', 'give')), 1, 0, 'L', true); $pdf->Cell(45, 6, utf8_decode(__('Number of Donations', 'give')), 1, 0, 'L', true); $pdf->Cell(35, 6, utf8_decode(__('Income to Date', 'give')), 1, 1, 'L', true); $year = date('Y'); $give_forms = get_posts(array('post_type' => 'give_forms', 'year' => $year, 'posts_per_page' => -1)); if ($give_forms) { $pdf->SetWidths(array(70, 30, 50, 50, 45, 35)); foreach ($give_forms as $form) { $pdf->SetFillColor(255, 255, 255); $title = $form->post_title; if (give_has_variable_prices($form->ID)) { $prices = give_get_variable_prices($form->ID); $first = $prices[0]['_give_amount']; $last = array_pop($prices); $last = $last['_give_amount']; if ($first < $last) { $min = $first; $max = $last; } else { $min = $last; $max = $first; } $price = html_entity_decode(give_currency_filter(give_format_amount($min)) . ' - ' . give_currency_filter(give_format_amount($max))); } else { $price = html_entity_decode(give_currency_filter(give_get_form_price($form->ID))); } $categories = get_the_term_list($form->ID, 'give_forms_category', '', ', ', ''); $categories = !is_wp_error($categories) ? strip_tags($categories) : ''; $tags = get_the_term_list($form->ID, 'give_forms_tag', '', ', ', ''); $tags = !is_wp_error($tags) ? strip_tags($tags) : ''; $sales = give_get_form_sales_stats($form->ID); $link = get_permalink($form->ID); $earnings = html_entity_decode(give_currency_filter(give_get_form_earnings_stats($form->ID))); if (function_exists('iconv')) { // Ensure characters like euro; are properly converted. $price = iconv('UTF-8', 'windows-1252', utf8_encode($price)); $earnings = iconv('UTF-8', 'windows-1252', utf8_encode($earnings)); } $pdf->Row(array($title, $price, $categories, $tags, $sales, $earnings)); } } else { $pdf->SetWidths(array(280)); $title = utf8_decode(sprintf(esc_html__('No %s found.', 'give'), give_get_forms_label_plural())); $pdf->Row(array($title)); } $pdf->Ln(); $pdf->SetTextColor(50, 50, 50); $pdf->SetFont('Helvetica', '', 14); $pdf->Cell(0, 10, utf8_decode(__('Graph View', 'give')), 0, 2, 'L', false); $pdf->SetFont('Helvetica', '', 12); $image = html_entity_decode(urldecode(give_draw_chart_image())); $image = str_replace(' ', '%20', $image); $pdf->SetX(25); $pdf->Image($image . '&file=.png'); $pdf->Ln(7); $pdf->Output(apply_filters('give_sales_earnings_pdf_export_filename', 'give-report-' . date_i18n('Y-m-d')) . '.pdf', 'D'); }
/** * Process Get Stats API Request * * @since 1.1 * * @global object $wpdb Used to query the database using the WordPress * * @param array $args Arguments provided by API Request * * @return array */ public function get_stats($args = array()) { $defaults = array('type' => null, 'form' => null, 'date' => null, 'startdate' => null, 'enddate' => null); $args = wp_parse_args($args, $defaults); $dates = $this->get_dates($args); $stats = array(); $earnings = array('earnings' => array()); $donations = array('donations' => array()); $error = array(); if (!user_can($this->user_id, 'view_give_reports') && !$this->override) { return $stats; } if ($args['type'] == 'donations') { if ($args['form'] == null) { if ($args['date'] == null) { $sales = $this->get_default_sales_stats(); } elseif ($args['date'] === 'range') { // Return sales for a date range // Ensure the end date is later than the start date if ($args['enddate'] < $args['startdate']) { $error['error'] = __('The end date must be later than the start date!', 'give'); } // Ensure both the start and end date are specified if (empty($args['startdate']) || empty($args['enddate'])) { $error['error'] = __('Invalid or no date range specified!', 'give'); } $total = 0; // Loop through the years $y = $dates['year']; while ($y <= $dates['year_end']) { if ($dates['year'] == $dates['year_end']) { $month_start = $dates['m_start']; $month_end = $dates['m_end']; } elseif ($y == $dates['year'] && $dates['year_end'] > $dates['year']) { $month_start = $dates['m_start']; $month_end = 12; } elseif ($y == $dates['year_end']) { $month_start = 1; $month_end = $dates['m_end']; } else { $month_start = 1; $month_end = 12; } $i = $month_start; while ($i <= $month_end) { if ($i == $dates['m_start']) { $d = $dates['day_start']; } else { $d = 1; } if ($i == $dates['m_end']) { $num_of_days = $dates['day_end']; } else { $num_of_days = cal_days_in_month(CAL_GREGORIAN, $i, $y); } while ($d <= $num_of_days) { $sale_count = give_get_sales_by_date($d, $i, $y); $date_key = date('Ymd', strtotime($y . '/' . $i . '/' . $d)); if (!isset($sales['sales'][$date_key])) { $sales['sales'][$date_key] = 0; } $sales['sales'][$date_key] += $sale_count; $total += $sale_count; $d++; } $i++; } $y++; } $sales['totals'] = $total; } else { if ($args['date'] == 'this_quarter' || $args['date'] == 'last_quarter') { $sales_count = 0; // Loop through the months $month = $dates['m_start']; while ($month <= $dates['m_end']) { $sales_count += give_get_sales_by_date(null, $month, $dates['year']); $month++; } $sales['donations'][$args['date']] = $sales_count; } else { $sales['donations'][$args['date']] = give_get_sales_by_date($dates['day'], $dates['m_start'], $dates['year']); } } } elseif ($args['form'] == 'all') { $forms = get_posts(array('post_type' => 'give_forms', 'nopaging' => true)); $i = 0; foreach ($forms as $form_info) { $sales['donations'][$i] = array($form_info->post_name => give_get_form_sales_stats($form_info->ID)); $i++; } } else { if (get_post_type($args['form']) == 'give_forms') { $form_info = get_post($args['form']); $sales['donations'][0] = array($form_info->post_name => give_get_form_sales_stats($args['form'])); } else { $error['error'] = sprintf(__('Product %s not found!', 'give'), $args['form']); } } if (!empty($error)) { return $error; } return $sales; } elseif ($args['type'] == 'earnings') { if ($args['form'] == null) { if ($args['date'] == null) { $earnings = $this->get_default_earnings_stats(); } elseif ($args['date'] === 'range') { // Return sales for a date range // Ensure the end date is later than the start date if ($args['enddate'] < $args['startdate']) { $error['error'] = __('The end date must be later than the start date!', 'give'); } // Ensure both the start and end date are specified if (empty($args['startdate']) || empty($args['enddate'])) { $error['error'] = __('Invalid or no date range specified!', 'give'); } $total = (double) 0.0; // Loop through the years $y = $dates['year']; if (!isset($earnings['earnings'])) { $earnings['earnings'] = array(); } while ($y <= $dates['year_end']) { if ($dates['year'] == $dates['year_end']) { $month_start = $dates['m_start']; $month_end = $dates['m_end']; } elseif ($y == $dates['year'] && $dates['year_end'] > $dates['year']) { $month_start = $dates['m_start']; $month_end = 12; } elseif ($y == $dates['year_end']) { $month_start = 1; $month_end = $dates['m_end']; } else { $month_start = 1; $month_end = 12; } $i = $month_start; while ($i <= $month_end) { if ($i == $dates['m_start']) { $d = $dates['day_start']; } else { $d = 1; } if ($i == $dates['m_end']) { $num_of_days = $dates['day_end']; } else { $num_of_days = cal_days_in_month(CAL_GREGORIAN, $i, $y); } while ($d <= $num_of_days) { $earnings_stat = give_get_earnings_by_date($d, $i, $y); $date_key = date('Ymd', strtotime($y . '/' . $i . '/' . $d)); if (!isset($earnings['earnings'][$date_key])) { $earnings['earnings'][$date_key] = 0; } $earnings['earnings'][$date_key] += $earnings_stat; $total += $earnings_stat; $d++; } $i++; } $y++; } $earnings['totals'] = $total; } else { if ($args['date'] == 'this_quarter' || $args['date'] == 'last_quarter') { $earnings_count = (double) 0.0; // Loop through the months $month = $dates['m_start']; while ($month <= $dates['m_end']) { $earnings_count += give_get_earnings_by_date(null, $month, $dates['year']); $month++; } $earnings['earnings'][$args['date']] = $earnings_count; } else { $earnings['earnings'][$args['date']] = give_get_earnings_by_date($dates['day'], $dates['m_start'], $dates['year']); } } } elseif ($args['form'] == 'all') { $forms = get_posts(array('post_type' => 'give_forms', 'nopaging' => true)); $i = 0; foreach ($forms as $form_info) { $earnings['earnings'][$i] = array($form_info->post_name => give_get_form_earnings_stats($form_info->ID)); $i++; } } else { if (get_post_type($args['form']) == 'give_forms') { $form_info = get_post($args['form']); $earnings['earnings'][0] = array($form_info->post_name => give_get_form_earnings_stats($args['form'])); } else { $error['error'] = sprintf(__('Form %s not found!', 'give'), $args['form']); } } if (!empty($error)) { return $error; } return $earnings; } elseif ($args['type'] == 'donors') { $customers = new Give_DB_Customers(); $stats['donations']['total_donors'] = $customers->count(); return $stats; } elseif (empty($args['type'])) { $stats = array_merge($stats, $this->get_default_sales_stats()); $stats = array_merge($stats, $this->get_default_earnings_stats()); return array('stats' => $stats); } }
/** * Retrieves the average monthly sales for a specific donation form * * @since 1.0 * * @param int $form_id Form ID * * @return float $sales Average monthly sales */ function give_get_average_monthly_form_sales($form_id = 0) { $sales = give_get_form_sales_stats($form_id); $release_date = get_post_field('post_date', $form_id); $diff = abs(current_time('timestamp') - strtotime($release_date)); $months = floor($diff / (30 * 60 * 60 * 24)); // Number of months since publication if ($months > 0) { $sales = $sales / $months; } return $sales; }
get_header(); //$form_id = 176; /** * give_before_main_content hook * * @hooked give_output_content_wrapper - 10 (outputs opening divs for the content) */ do_action('give_before_main_content'); while (have_posts()) { the_post(); $form_id = get_the_ID(); $content = wpautop(get_post_meta($form_id, '_give_form_content', true)); $amount_goal = number_format(get_post_meta($form_id, '_give_set_goal', true), 2, '.', ''); $amount_have = number_format(get_post_meta($form_id, '_give_form_earnings', true), 2, '.', ''); $width_bar = round($amount_have / $amount_goal * 100, 2); $number_donations = give_get_form_sales_stats($form_id); $give_vimeo = get_post_meta($post->ID, 'vimeo', 1); $give_youtube = get_post_meta($post->ID, 'youtube', 1); // This is operation for display data in the get_student_thumbnail() $user_id = get_current_user_id($user_id); $user_data = get_userdata($user_id); //login data of the current user $author_login = $user_data->user_login; //Login data in meta data in the wish $login_wish = get_post_meta($form_id, 'autor_login', true); if ($author_login === $login_wish) { $display_author_info = true; } else { $display_author_info = false; } ?>
/** * Build all the reports data * * @access public * @since 1.0 * @return array $reports_data All the data for donor reports */ public function reports_data() { $reports_data = array(); $give_forms = $this->products->posts; if ($give_forms) { foreach ($give_forms as $form) { $reports_data[] = array('ID' => $form, 'title' => get_the_title($form), 'sales' => give_get_form_sales_stats($form), 'earnings' => give_get_form_earnings_stats($form), 'average_sales' => give_get_average_monthly_form_sales($form), 'average_earnings' => give_get_average_monthly_form_earnings($form)); } } return $reports_data; }
/** * Decrement the sale count by one * * @since 1.0 * @access public * * @param int $quantity The quantity to decrease by. Default is 1. * * @return int|false New number of total sales. */ public function decrease_sales($quantity = 1) { $sales = give_get_form_sales_stats($this->ID); // Only decrease if not already zero if ($sales > 0) { $quantity = absint($quantity); $total_sales = $sales - $quantity; if ($this->update_meta('_give_form_sales', $total_sales)) { $this->sales = $sales; return $sales; } } return false; }
/** * Decrement the sale count by one * * @since 1.0 * @return int|false */ public function decrease_sales() { $sales = give_get_form_sales_stats($this->ID); if ($sales > 0) { $sales = $sales - 1; } if (update_post_meta($this->ID, '_give_form_sales', $sales)) { $this->sales = $sales; return $sales; } return false; }