PluginProbe
Independent Analytics – WordPress Analytics Plugin / 1.24.1
Independent Analytics – WordPress Analytics Plugin v1.24.1
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 All 120 releases
independent-analytics / IAWP / Campaign_Builder.php
Campaign_Builder.php
99 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace IAWP_SCOPED\IAWP;
4
5 use IAWP_SCOPED\IAWP\Utils\Singleton;
6 use IAWP_SCOPED\IAWP\Utils\String_Util;
7 use IAWP_SCOPED\IAWP\Utils\URL;
8 use IAWP_SCOPED\League\Uri\Uri;
9 class Campaign_Builder
10 {
11 use Singleton;
12 public function __construct()
13 {
14 }
15 private function time_ago($time)
16 {
17 $time_difference = \time() - $time;
18 if ($time_difference < 1) {
19 return 'less than 1 second ago';
20 }
21 $condition = [12 * 30 * 24 * 60 * 60 => 'year', 30 * 24 * 60 * 60 => 'month', 24 * 60 * 60 => 'day', 60 * 60 => 'hour', 60 => 'minute', 1 => 'second'];
22 foreach ($condition as $secs => $str) {
23 $d = $time_difference / $secs;
24 if ($d >= 1) {
25 $t = \round($d);
26 return $t . ' ' . $str . ($t > 1 ? 's' : '') . ' ago';
27 }
28 }
29 }
30 private function get_previously_created_campaigns()
31 {
32 global $wpdb;
33 $campaign_urls_table = Query::get_table_name(Query::CAMPAIGN_URLS);
34 $results = $wpdb->get_results("\n SELECT * FROM {$campaign_urls_table} ORDER BY created_at DESC LIMIT 100\n ");
35 return \array_map(function ($result) {
36 $created_at = new \DateTime($result->created_at);
37 $time_ago = $this->time_ago($created_at->getTimestamp());
38 return ['result' => \json_encode((array) $result), 'created_at' => $time_ago, 'url' => $this->build_url($result->path, $result->utm_source, $result->utm_medium, $result->utm_campaign, $result->utm_term, $result->utm_content)];
39 }, $results);
40 }
41 public function render_campaign_builder()
42 {
43 echo \IAWP_SCOPED\iawp()->templates()->render('campaign_builder', ['campaigns' => $this->get_previously_created_campaigns()]);
44 }
45 public function create_campaign($path, $source, $medium, $campaign, $term, $content)
46 {
47 global $wpdb;
48 $has_errors = \false;
49 $path = \strlen($path) > 0 ? $path : '';
50 $path_error = null;
51 $source_error = null;
52 $medium_error = null;
53 $campaign_error = null;
54 $term = \strlen($term) > 0 ? $term : null;
55 $content = \strlen($content) > 0 ? $content : null;
56 $url = new URL(\site_url() . $path);
57 if (!$url->is_valid_url()) {
58 $has_errors = \true;
59 $path_error = 'path invalid';
60 }
61 if (\strlen($source) === 0) {
62 $has_errors = \true;
63 $source_error = 'Source is required';
64 }
65 if (\strlen($medium) === 0) {
66 $has_errors = \true;
67 $medium_error = 'Medium is required';
68 }
69 if (\strlen($campaign) === 0) {
70 $has_errors = \true;
71 $campaign_error = 'Campaign is required';
72 }
73 if ($has_errors) {
74 return \IAWP_SCOPED\iawp()->templates()->render('campaign_builder', ['path' => $path, 'path_error' => $path_error, 'utm_source' => $source, 'utm_source_error' => $source_error, 'utm_medium' => $medium, 'utm_medium_error' => $medium_error, 'utm_campaign' => $campaign, 'utm_campaign_error' => $campaign_error, 'utm_term' => $term, 'utm_content' => $content, 'campaigns' => $this->get_previously_created_campaigns()]);
75 }
76 $campaign_urls_table = Query::get_table_name(Query::CAMPAIGN_URLS);
77 $wpdb->insert($campaign_urls_table, ['path' => $path, 'utm_source' => $source, 'utm_medium' => $medium, 'utm_campaign' => $campaign, 'utm_term' => $term, 'utm_content' => $content, 'created_at' => (new \DateTime())->format('Y-m-d H:i:s')]);
78 $url = $this->build_url($path, $source, $medium, $campaign, $term, $content);
79 return \IAWP_SCOPED\iawp()->templates()->render('campaign_builder', ['path' => $path, 'utm_source' => $source, 'utm_medium' => $medium, 'utm_campaign' => $campaign, 'utm_term' => $term, 'utm_content' => $content, 'new_campaign_url' => $url, 'campaigns' => $this->get_previously_created_campaigns()]);
80 }
81 public function build_url($path, $source, $medium, $campaign, $term = null, $content = null) : string
82 {
83 $path = String_Util::str_starts_with($path, '/') ? \substr($path, 1) : $path;
84 $uri = Uri::createFromString(\trailingslashit(\site_url()) . $path);
85 $existing_query = $uri->getQuery();
86 \parse_str($existing_query, $existing_query);
87 $existing_query['utm_source'] = $source;
88 $existing_query['utm_medium'] = $medium;
89 $existing_query['utm_campaign'] = $campaign;
90 if (isset($term)) {
91 $existing_query['utm_term'] = $term;
92 }
93 if (isset($content)) {
94 $existing_query['utm_content'] = $content;
95 }
96 return $uri->withQuery(\http_build_query($existing_query));
97 }
98 }
99