AJAX
3 years ago
Date_Range
3 years ago
Interval
3 years ago
Menu_Bar_Stats
3 years ago
Migrations
3 years ago
Models
3 years ago
Queries
3 years ago
Statistics
3 years ago
Tables
3 years ago
Utils
3 years ago
Campaign_Builder.php
3 years ago
Capability_Manager.php
3 years ago
Chart.php
3 years ago
Chart_Geo.php
3 years ago
Chart_SVG.php
3 years ago
Dashboard_Options.php
3 years ago
Dashboard_Widget.php
3 years ago
Email_Reports.php
3 years ago
Filters.php
3 years ago
Geo_Database.php
3 years ago
Geo_Database_Download_Job.php
3 years ago
Health_Check.php
3 years ago
Illuminate_Builder.php
3 years ago
Independent_Analytics.php
3 years ago
Known_Referrers.php
3 years ago
PDF.php
3 years ago
Query.php
3 years ago
Quick_Stats.php
3 years ago
REST_API.php
3 years ago
Real_Time.php
3 years ago
Reset_Database.php
3 years ago
Resource_Identifier.php
3 years ago
Settings.php
3 years ago
Track_Resource_Changes.php
3 years ago
View.php
3 years ago
View_Counter.php
3 years ago
WP_Option_Cache_Bust.php
3 years ago
WooCommerce_Order.php
3 years ago
PDF.php
98 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP_SCOPED\IAWP; |
| 4 | |
| 5 | use IAWP_SCOPED\Dompdf\Dompdf; |
| 6 | use IAWP_SCOPED\Dompdf\Options; |
| 7 | use IAWP_SCOPED\IAWP\Date_Range\Exact_Date_Range; |
| 8 | use IAWP_SCOPED\IAWP\Date_Range\Relative_Date_Range; |
| 9 | use IAWP_SCOPED\IAWP\Queries\Country_Statistics; |
| 10 | use IAWP_SCOPED\IAWP\Queries\Referrers; |
| 11 | use IAWP_SCOPED\IAWP\Queries\Resources; |
| 12 | use IAWP_SCOPED\IAWP\Statistics\Page_Statistics; |
| 13 | require_once \ABSPATH . 'wp-admin/includes/file.php'; |
| 14 | class PDF |
| 15 | { |
| 16 | public function create_and_save_pdf() |
| 17 | { |
| 18 | $html = $this->get_pdf_html(); |
| 19 | $pdf = $this->generate_pdf($html); |
| 20 | $file_path = $this->save_pdf_file($pdf); |
| 21 | return $file_path; |
| 22 | } |
| 23 | private function get_pdf_html() |
| 24 | { |
| 25 | $date_range = new Relative_Date_Range('LAST_MONTH'); |
| 26 | $statistics = new Page_Statistics($date_range); |
| 27 | $style_url = \IAWP_SCOPED\iawp_url_to('dist/styles/pdf.css'); |
| 28 | $html = \IAWP_SCOPED\iawp()->templates()->render('pdf/site-overview', ['title' => \get_bloginfo('name'), 'date' => (new \DateTime('Last month'))->format('F Y'), 'stats' => (new Quick_Stats($statistics, null))->get_html(), 'chart_data' => (new Chart_SVG($statistics, 700, 300))->get_svg_data(), 'top_ten' => $this->get_top_ten(), 'analytics_url' => \esc_url(\IAWP_SCOPED\iawp_dashboard_url()), 'style_url' => $style_url]); |
| 29 | return $html; |
| 30 | } |
| 31 | private function generate_pdf($html) : string |
| 32 | { |
| 33 | $options = new Options(); |
| 34 | $options->set('defaultFont', 'Helvetica'); |
| 35 | $options->set('isRemoteEnabled', \true); |
| 36 | $dompdf = new Dompdf($options); |
| 37 | $dompdf->loadHtml($html); |
| 38 | $dompdf->setPaper('A4', 'portrait'); |
| 39 | $dompdf->render(); |
| 40 | return $dompdf->output(); |
| 41 | } |
| 42 | private function save_pdf_file($pdf) : ?string |
| 43 | { |
| 44 | $upload = \wp_upload_bits('site-report.pdf', null, $pdf); |
| 45 | if ($upload['error']) { |
| 46 | return null; |
| 47 | } |
| 48 | $destination_file = \trailingslashit(\wp_upload_dir()['basedir']) . 'site-report.pdf'; |
| 49 | global $wp_filesystem; |
| 50 | \WP_Filesystem(); |
| 51 | $success = $wp_filesystem->move($upload['file'], $destination_file, \true); |
| 52 | if ($success) { |
| 53 | return $destination_file; |
| 54 | } else { |
| 55 | return null; |
| 56 | } |
| 57 | } |
| 58 | private function get_top_ten() : array |
| 59 | { |
| 60 | $start = new \DateTime('First day of last month'); |
| 61 | $end = new \DateTime('Last day of last month'); |
| 62 | $date_range = new Exact_Date_Range($start, $end); |
| 63 | $queries = ['pages' => 'title', 'referrers' => 'referrer', 'geo' => 'country']; |
| 64 | $top_ten = []; |
| 65 | foreach ($queries as $type => $title) { |
| 66 | if ($type === 'pages') { |
| 67 | $query = new Resources($date_range); |
| 68 | } elseif ($type === 'referrers') { |
| 69 | $query = new Referrers($date_range); |
| 70 | } elseif ($type === 'geo') { |
| 71 | $query = new Country_Statistics($date_range); |
| 72 | } else { |
| 73 | continue; |
| 74 | } |
| 75 | $data = $query->fetch(); |
| 76 | \usort($data, function ($a, $b) { |
| 77 | $a = $a->views(); |
| 78 | $b = $b->views(); |
| 79 | if ($a < $b) { |
| 80 | return 1; |
| 81 | } elseif ($a > $b) { |
| 82 | return -1; |
| 83 | } else { |
| 84 | return 0; |
| 85 | } |
| 86 | }); |
| 87 | $data = \array_slice($data, 0, 10); |
| 88 | $rows = \array_map(function ($row, $index) use($title) { |
| 89 | $edited_title = $row->{$title}(); |
| 90 | $edited_title = \mb_strlen($edited_title) > 26 ? \mb_substr($edited_title, 0, 26) . '...' : $edited_title; |
| 91 | return ['title' => $edited_title, 'views' => $row->views()]; |
| 92 | }, $data, \array_keys($data)); |
| 93 | $top_ten[$type] = $rows; |
| 94 | } |
| 95 | return $top_ten; |
| 96 | } |
| 97 | } |
| 98 |