Skip to content

Commit f800357

Browse files
committed
Implement tracking of page title
User has a feature request to show the page title instead of just the target url. After some long discussions we decided to introduce a new statify meta table. In this table any additional tracking data can be saved. With this PR we save the wp_document_title in this meta table.
1 parent c5c21ba commit f800357

File tree

9 files changed

+277
-147
lines changed

9 files changed

+277
-147
lines changed

inc/class-statify-frontend.php

Lines changed: 104 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,47 @@
1818
*/
1919
class Statify_Frontend extends Statify {
2020

21+
/**
22+
* Statify meta fields for tracking
23+
*
24+
* @var array
25+
*/
26+
private static $tracking_meta = array();
27+
28+
/**
29+
* Default statify tracking data
30+
*
31+
* @var array
32+
*/
33+
private static $tracking_data = array();
34+
35+
/**
36+
* Initialization of tracking data
37+
*
38+
* @return void
39+
*/
40+
public static function init_tracking_data() {
41+
self::$tracking_data['target'] = isset( $_SERVER['REQUEST_URI'] )
42+
? filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ), FILTER_SANITIZE_URL )
43+
: '/';
44+
45+
self::$tracking_data['referrer'] = isset( $_SERVER['HTTP_REFERER'] )
46+
? filter_var( wp_unslash( $_SERVER['HTTP_REFERER'] ), FILTER_SANITIZE_URL )
47+
: '';
48+
49+
self::$tracking_data = apply_filters( 'statify__tracking_data', self::$tracking_data );
50+
51+
self::$tracking_meta = array(
52+
array(
53+
'meta_key' => 'title',
54+
'meta_value' => wp_get_document_title(),
55+
'type' => 'text',
56+
'sanitize_callback' => 'sanitize_text_field',
57+
),
58+
);
59+
self::$tracking_meta = apply_filters( 'statify__tracking_meta', self::$tracking_meta, self::$tracking_data );
60+
}
61+
2162
/**
2263
* Track the page view
2364
*
@@ -30,51 +71,55 @@ class Statify_Frontend extends Statify {
3071
* @return boolean
3172
*/
3273
public static function track_visit( $is_snippet = false ) {
33-
// Set target & referrer.
34-
$target = null;
35-
$referrer = null;
74+
if ( empty( self::$tracking_data ) ) {
75+
self::init_tracking_data();
76+
}
77+
3678
if ( self::is_javascript_tracking_enabled() ) {
3779
if ( ! $is_snippet ) {
3880
return false;
3981
}
4082

41-
if ( isset( $_REQUEST['statify_target'] ) ) {
42-
$target = filter_var( wp_unslash( $_REQUEST['statify_target'] ), FILTER_SANITIZE_URL );
83+
$json = file_get_contents( 'php://input' );
84+
$raw_data = json_decode( $json, true );
85+
if ( ! $raw_data || ! isset( $raw_data['statify_tracking_data'] ) ) {
86+
return false;
4387
}
44-
if ( isset( $_REQUEST['statify_referrer'] ) ) {
45-
$referrer = filter_var( wp_unslash( $_REQUEST['statify_referrer'] ), FILTER_SANITIZE_URL );
88+
89+
$tracking_data = array(
90+
'target' =>
91+
isset( $raw_data['statify_tracking_data']['target'] )
92+
? filter_var( wp_unslash( $raw_data['statify_tracking_data']['target'] ), FILTER_SANITIZE_URL )
93+
: '/',
94+
'referrer' =>
95+
isset( $raw_data['statify_tracking_data']['referrer'] )
96+
? filter_var( wp_unslash( $raw_data['statify_tracking_data']['referrer'] ), FILTER_SANITIZE_URL )
97+
: '',
98+
);
99+
100+
$tracking_meta = array();
101+
if ( isset( $raw_data['statify_tracking_meta'] ) && is_array( $raw_data['statify_tracking_meta'] ) ) {
102+
$tracking_meta = $raw_data['statify_tracking_meta'];
46103
}
47104
} else {
48-
if ( isset( $_SERVER['REQUEST_URI'] ) ) {
49-
$target = filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ), FILTER_SANITIZE_URL );
50-
}
51-
if ( isset( $_SERVER['HTTP_REFERER'] ) ) {
52-
$referrer = filter_var( wp_unslash( $_SERVER['HTTP_REFERER'] ), FILTER_SANITIZE_URL );
53-
}
105+
$tracking_data = self::$tracking_data;
106+
$tracking_meta = wp_list_pluck( self::$tracking_meta, 'meta_value', 'meta_key' );
54107
}
55108

56-
// Fallbacks for uninitialized or omitted target and referrer values.
57-
if ( is_null( $target ) || false === $target ) {
58-
$target = '/';
59-
}
60-
if ( is_null( $referrer ) || false === $referrer ) {
61-
$referrer = '';
62-
}
63-
64-
/* Invalid target? */
65-
if ( empty( $target ) || ! wp_validate_redirect( $target, false ) ) {
109+
// Invalid target.
110+
if ( ! wp_validate_redirect( $tracking_data['target'], false ) ) {
66111
return self::_jump_out( $is_snippet );
67112
}
68113

69-
/* Check whether tracking should be skipped for this view. */
114+
// Check whether tracking should be skipped for this view.
70115
if ( self::_skip_tracking() ) {
71116
return self::_jump_out( $is_snippet );
72117
}
73118

74-
/* Global vars */
119+
// Global vars.
75120
global $wpdb, $wp_rewrite;
76121

77-
/* Init rows */
122+
// Init rows.
78123
$data = array(
79124
'created' => '',
80125
'referrer' => '',
@@ -87,24 +132,49 @@ public static function track_visit( $is_snippet = false ) {
87132
$needles = array( home_url(), network_admin_url() );
88133

89134
// Sanitize referrer url.
90-
if ( ! empty( $referrer ) && self::strposa( $referrer, $needles ) === false ) {
91-
$data['referrer'] = esc_url_raw( $referrer, array( 'http', 'https' ) );
135+
if ( self::strposa( $tracking_data['referrer'], $needles ) === false ) {
136+
$data['referrer'] = filter_var( $tracking_data['referrer'], FILTER_SANITIZE_URL );
137+
$data['referrer'] = esc_url_raw( $data['referrer'], array( 'http', 'https' ) );
92138
}
93139

94-
/* Relative target url */
95-
$data['target'] = user_trailingslashit( str_replace( home_url( '/', 'relative' ), '/', $target ) );
140+
// Relative target url.
141+
$data['target'] = filter_var( $tracking_data['target'], FILTER_SANITIZE_URL );
142+
$data['target'] = user_trailingslashit( str_replace( home_url( '/', 'relative' ), '/', $data['target'] ) );
96143

97144
// Trim target url.
98145
if ( $wp_rewrite->permalink_structure ) {
99146
$data['target'] = wp_parse_url( $data['target'], PHP_URL_PATH );
100147
}
101148

102-
// Sanitize target url.
149+
// Escaping target url.
103150
$data['target'] = esc_url_raw( $data['target'] );
104151

105152
// Insert.
106153
$wpdb->insert( $wpdb->statify, $data );
107154

155+
$statify_id = $wpdb->insert_id;
156+
157+
foreach ( self::$tracking_meta as $meta_field ) {
158+
if ( array_key_exists( $meta_field['meta_key'], $tracking_meta ) ) {
159+
$meta_value = $tracking_meta[ $meta_field['meta_key'] ];
160+
161+
$sanitize_function = isset( $meta_field['sanitize_callback'] ) && is_callable( $meta_field['sanitize_callback'] )
162+
? $meta_field['sanitize_callback']
163+
: 'sanitize_text_field';
164+
165+
$meta_value = call_user_func( $sanitize_function, $meta_value );
166+
167+
// Init rows.
168+
$data = array(
169+
'statify_id' => $statify_id,
170+
'meta_key' => $meta_field['meta_key'],
171+
'meta_value' => $meta_value,
172+
);
173+
174+
$wpdb->insert( $wpdb->statifymeta, $data );
175+
}
176+
}
177+
108178
/**
109179
* Fires after a visit was stored in the database
110180
*
@@ -374,13 +444,15 @@ public static function wp_footer() {
374444
true
375445
);
376446

377-
// Add endpoint to script.
447+
// Add endpoint and tracking_information to script.
378448
wp_localize_script(
379449
'statify-js',
380450
'statify_ajax',
381451
array(
382452
'url' => admin_url( 'admin-ajax.php' ),
383453
'nonce' => wp_create_nonce( 'statify_track' ),
454+
'tracking_data' => self::$tracking_data,
455+
'tracking_meta' => wp_list_pluck( self::$tracking_meta, 'meta_value', 'meta_key' ),
384456
)
385457
);
386458
}

inc/class-statify-install.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ private static function _apply() {
8282
);
8383
}
8484

85-
// Create the actual tables.
86-
Statify_Table::init();
87-
Statify_Table::create();
85+
// Initialize the database schema.
86+
Statify_Schema::init();
8887
}
8988
}

inc/class-statify-schema.php

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
/**
3+
* Statify: Statify_Schema class
4+
*
5+
* This file contains class for the plugin's DB scheme handling.
6+
*
7+
* @package Statify
8+
* @since 2.0.0
9+
*/
10+
11+
// Quit if accessed outside WP context.
12+
defined( 'ABSPATH' ) || exit;
13+
14+
/**
15+
* Statify DB Schema
16+
*
17+
* @since 2.0.0
18+
*/
19+
class Statify_Schema {
20+
/**
21+
* Database tables
22+
*
23+
* @var string[]
24+
*/
25+
public static $tables = array(
26+
'statify',
27+
'statifymeta',
28+
);
29+
30+
/**
31+
* Needed statify db version for current plugin version
32+
*
33+
* @var string
34+
*/
35+
public static $db_version = '2.0.0';
36+
37+
/**
38+
* Definition of the custom tables.
39+
*
40+
* @since 2.0.0
41+
* @version 2.0.0
42+
*/
43+
public static function init() {
44+
// Global.
45+
global $wpdb;
46+
47+
foreach ( static::$tables as $table ) {
48+
$wpdb->tables[] = $table;
49+
$wpdb->$table = $wpdb->get_blog_prefix() . $table;
50+
}
51+
52+
self::maybe_create_tables();
53+
}
54+
55+
/**
56+
* Create the tables.
57+
*
58+
* @since 2.0.0
59+
* @version 2.0.0
60+
*/
61+
public static function maybe_create_tables() {
62+
$current_db_version = get_option( 'statify_db_version', '1.8.4' );
63+
if ( $current_db_version === self::$db_version ) {
64+
return;
65+
}
66+
67+
// Global.
68+
global $wpdb, $charset_collate;
69+
70+
/**
71+
* Use same index length like the WordPress core
72+
*
73+
* @see wp_get_db_schema()
74+
*/
75+
$max_index_length = 191;
76+
77+
// Include.
78+
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
79+
80+
// Create statify table.
81+
dbDelta(
82+
"CREATE TABLE {$wpdb->statify} (
83+
id bigint(20) unsigned NOT NULL auto_increment,
84+
created date NOT NULL default '0000-00-00',
85+
referrer varchar(255) NOT NULL default '',
86+
target varchar(255) NOT NULL default '',
87+
PRIMARY KEY (id),
88+
KEY referrer (referrer),
89+
KEY target (target),
90+
KEY created (created)
91+
) {$charset_collate};"
92+
);
93+
94+
// Create statifymeta table.
95+
dbDelta(
96+
"CREATE TABLE {$wpdb->statifymeta} (
97+
meta_id bigint(20) unsigned NOT NULL auto_increment,
98+
statify_id bigint(20) unsigned NOT NULL default 0,
99+
meta_key varchar(255) default NULL,
100+
meta_value longtext,
101+
PRIMARY KEY (meta_id),
102+
KEY statify_id (statify_id),
103+
KEY meta_key (meta_key({$max_index_length}))
104+
) {$charset_collate};"
105+
);
106+
107+
update_option( 'statify_db_version', self::$db_version );
108+
}
109+
110+
/**
111+
* Remove the custom tables.
112+
*
113+
* @since 2.0.0
114+
* @version 2.0.0
115+
*/
116+
public static function drop_tables() {
117+
global $wpdb;
118+
119+
// Remove.
120+
foreach ( static::$tables as $table ) {
121+
$wpdb->query( "DROP TABLE IF EXISTS `{$wpdb->$table}`" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
122+
}
123+
124+
delete_option( 'statify_db_version' );
125+
}
126+
}

0 commit comments

Comments
 (0)