-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-tracking.php
168 lines (145 loc) · 5.43 KB
/
class-tracking.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
* @package Admin
*/
/**
* Class that creates the tracking functionality for WP SEO, as the core class might be used in more plugins, it's checked for existence first.
*/
if ( !class_exists( 'Yoast_Tracking' ) ) {
class Yoast_Tracking {
/**
* Class constructor
*/
function __construct() {
add_action( 'admin_footer', array( $this, 'tracking' ), 99 );
// Invalidate the cache when changes are being made.
add_action( 'switch_theme', array( $this, 'delete_cache' ) );
add_action( 'admin_init', array( $this, 'check_active_plugins' ) );
}
/**
* This is the only current way of doing something when a plugin is activated or updated...
*/
function check_active_plugins() {
$hash = md5( serialize( get_option( 'active_plugins' ) ) );
$old_hash = get_transient( 'yoast_tracking_active_plugins_hash' );
if ( $hash != $old_hash ) {
add_action( 'admin_footer', array( $this, 'delete_cache' ) );
set_transient( 'yoast_tracking_active_plugins_hash', $hash, 7 * 60 * 60 * 24 );
}
}
/**
* This deletes the tracking cache, effectively meaning the tracking will be done again.
*/
function delete_cache() {
delete_transient( 'yoast_tracking_cache' );
}
/**
* Main tracking function.
*/
function tracking() {
global $pagenow;
if ( in_array( $pagenow, array('index.php','plugins.php','update-core.php','themes.php') ) === false )
return;
// Start of Metrics
global $wpdb;
$options = get_option( 'wpseo' );
if ( !isset( $options['hash'] ) || empty( $options['hash'] ) ) {
$options['hash'] = md5( site_url() );
update_option( 'wpseo', $options );
}
$data = get_transient( 'yoast_tracking_cache' );
if ( !$data ) {
$pts = array();
foreach ( get_post_types( array( 'public' => true ) ) as $pt ) {
$count = wp_count_posts( $pt );
$pts[$pt] = $count->publish;
}
$comments_count = wp_count_comments();
// wp_get_theme was introduced in 3.4, for compatibility with older versions, let's do a workaround for now.
if ( function_exists( 'wp_get_theme' ) ) {
$theme_data = wp_get_theme();
$theme = array(
'name' => $theme_data->display( 'Name', false, false ),
'theme_uri' => $theme_data->display( 'ThemeURI', false, false ),
'version' => $theme_data->display( 'Version', false, false ),
'author' => $theme_data->display( 'Author', false, false ),
'author_uri'=> $theme_data->display( 'AuthorURI', false, false ),
);
if ( isset( $theme_data->template ) && !empty( $theme_data->template ) && $theme_data->parent() ) {
$theme['template'] = array(
'version' => $theme_data->parent()->display( 'Version', false, false ),
'name' => $theme_data->parent()->display( 'Name', false, false ),
'theme_uri' => $theme_data->parent()->display( 'ThemeURI', false, false ),
'author' => $theme_data->parent()->display( 'Author', false, false ),
'author_uri'=> $theme_data->parent()->display( 'AuthorURI', false, false ),
);
} else {
$theme['template'] = '';
}
} else {
$theme_data = (object) get_theme_data( get_stylesheet_directory() . '/style.css' );
$theme = array(
'version' => $theme_data->Version,
'name' => $theme_data->Name,
'author' => $theme_data->Author,
'template' => $theme_data->Template,
);
}
$plugins = array();
foreach ( get_option( 'active_plugins' ) as $plugin_path ) {
$plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin_path );
$slug = str_replace( '/' . basename( $plugin_path ), '', $plugin_path );
$plugins[$slug] = array(
'version' => $plugin_info['Version'],
'name' => $plugin_info['Name'],
'plugin_uri' => $plugin_info['PluginURI'],
'author' => $plugin_info['AuthorName'],
'author_uri' => $plugin_info['AuthorURI'],
);
}
$data = array(
'site' => array(
'hash' => $options['hash'],
'url' => site_url(),
'name' => get_bloginfo( 'name' ),
'version' => get_bloginfo( 'version' ),
'multisite' => is_multisite(),
'users' => count( get_users() ),
'lang' => get_locale(),
),
'pts' => $pts,
'comments' => array(
'total' => $comments_count->total_comments,
'approved' => $comments_count->approved,
'spam' => $comments_count->spam,
'pings' => $wpdb->get_var( "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_type = 'pingback'" ),
),
'options' => apply_filters( 'yoast_tracking_filters', array() ),
'theme' => $theme,
'plugins' => $plugins,
);
$args = array(
'body' => $data
);
wp_remote_post( 'https://tracking.yoast.com/', $args );
// Store for a week, then push data again.
set_transient( 'yoast_tracking_cache', $data, ( 7 * 60 * 60 * 24 ) );
}
}
}
$yoast_tracking = new Yoast_Tracking;
}
/**
* Adds tracking parameters for Google Analytics settings. Outside of the main class as the class could also be in use in other plugins.
*
* @param array $options
* @return array
*/
function ystga_tracking_additions( $options ) {
$opt = get_option('Yoast_Google_Analytics');
$options['gawp'] = array(
'advanced_settigns' => isset( $opt['advancedsettings'] ) ? 1 : 0,
);
return $options;
}
add_filter( 'yoast_tracking_filters', 'ystga_tracking_additions' );