PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 1.23.0
Independent Analytics – WordPress Analytics Plugin v1.23.0
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 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / IAWP / Campaign_Builder.php
independent-analytics / IAWP Last commit date
AJAX 3 years ago Date_Range 3 years ago Interval 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 Current_Resource.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 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
Campaign_Builder.php
99 lines
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