-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsranalytics.php
207 lines (172 loc) · 6.91 KB
/
sranalytics.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
Plugin Name: SimpleReach Analytics
Plugin URI: http://www.simplereach.com/docs/wordpress-plugin/
Text Domain: sranalytics
Description: After installation, you must click '<a href='options-general.php?page=SimpleReach-Analytics'>Settings → SimpleReach Analytics</a>' to turn on the Analytics.
Version: 0.1.4
Author: SimpleReach
Author URI: https://www.simplereach.com
*/
/* Copyright 2014 SimpleReach (email : support@simplereach.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
define( 'SRANALYTICS_PLUGIN_VERSION', '0.1.4' );
/**
* Insert analytics code onto the post page
*/
function sranalytics_insert_js() {
global $post;
// Do not show SimpleReach tags by default
$sranalytics_show_beacon = false;
// Get the options
$sranalytics_pid = get_option( 'sranalytics_pid' );
$sranalytics_show_on_tac_pages = get_option( 'sranalytics_show_on_tac_pages' );
$sranalytics_show_on_wp_pages = get_option( 'sranalytics_show_on_wp_pages' );
$sranalytics_show_on_attachment_pages = get_option( 'sranalytics_show_on_attachment_pages' );
$sranalytics_show_everywhere = get_option( 'sranalytics_show_everywhere' );
$sranalytics_force_http = get_option( 'sranalytics_force_http' );
// Try and check the validity of the PID
if ( empty( $sranalytics_pid) || 24 != strlen( $sranalytics_pid ) ) {
return False;
}
// Show everywhere
if ( $sranalytics_show_everywhere ) {
$sranalytics_show_beacon = true;
}
//Show on attachment pages if option set
if ( is_attachment() && $sranalytics_show_on_attachment_pages ) {
$sranalytics_show_beacon = true;
}
// Ensure we show on post pages
if ( is_single() && !is_attachment() ) {
$sranalytics_show_beacon = true;
}
// Ensure we show on WP pages if we are supposed to
if ( is_page() && $sranalytics_show_on_wp_pages ) {
$sranalytics_show_beacon = true;
}
// Ensure we show on WP pages if we are supposed to
if ( is_page() && $sranalytics_show_on_wp_pages ) {
$sranalytics_show_beacon = true;
}
$post_id = $post->ID;
// If the post isn't published yet, don't show the __reach_config
// attachments don't have published status though so always show for them.
if ( 'publish' != $post->post_status && !is_attachment() ) {
return False;
}
// default case of a regular post
$title = $post->post_title;
$authors = array( get_the_author_meta('user_nicename', $post->post_author ) );
$tags = wp_get_post_tags( $post->ID, array( 'fields' => 'names' ) );
$channels = wp_get_post_categories( $post->ID, array( 'fields' => 'slugs' ) );
$published_date = $post->post_date_gmt;
$canonical_url = get_permalink( $post->ID );
// Show the tags if we are on a tag/author/category page and we are supposed to
if ( ( is_category() || is_author() || is_tag() ) && ( $sranalytics_show_on_tac_pages || $sranalytics_show_everywhere ) ) {
$sranalytics_show_beacon = true;
$channels = array();
$authors = array();
$tags = array();
//handle archive-style pages. WordPress has a different pattern for retrieving each one
if ( is_tag() ) {
$tag_name = single_cat_title( '', false );
if ( function_exists( 'wpcom_vip_get_term_by' ) ) {
$tag = wpcom_vip_get_term_by( 'name', $tag_name, 'post_tag' );
} else {
$tag = get_term_by( 'name', $tag_name, 'post_tag' );
}
$tag_url = get_tag_link($tag->term_id);
$title = "Tag: ${tag_name}";
$tags[] = $tag_name;
$canonical_url = $tag_url;
} elseif ( is_author() ) {
$author_id = get_the_author_meta( 'ID' );
$author_name = get_the_author();
$title = "Author: ${author_name}";
$authors[] = $author_name;
$canonical_url = get_author_posts_url( $author_id );
} elseif ( is_category() ) {
$channel_name = single_cat_title( '', false );
$category_id = get_cat_ID( $channel_name );
$title = "Category: ${channel_name}";
$channels[] = $channel_name;
$canonical_url = get_category_link( $category_id );
} else {
// We should NEVER get here
$title = "Unkown Page Type";
}
// If we are on a page, then we need to add it
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
if ($paged > 1) {
$title = "${title} - Page ${paged}";
}
}
// Handle the homepage properly if we are supposed to fire on it
if ( (is_home() || is_page( 'home' ) ) && $sranalytics_show_everywhere ) {
$title = "Homepage";
$channels = array();
$authors = array();
$tags = array();
$canonical_url = get_home_url();
}
//force https to http if option is checked
if($sranalytics_force_http){
$pattern = '/^https:\/\//';
$canonical_url = preg_replace( $pattern , "http://" , $canonical_url);
}
//prepare and escape all JS variables
$javascript_array = array(
'version' => SRANALYTICS_PLUGIN_VERSION,
'pid' => esc_js( $sranalytics_pid ),
'title' => esc_js( apply_filters( 'sranalytics_title', $title ) ),
'url' => esc_js( apply_filters( 'sranalytics_url', $canonical_url ) ),
'date' => esc_js( apply_filters( 'sranalytics_date', $published_date ) ),
'channels' => array_map( 'esc_js', apply_filters( 'sranalytics_channels', $channels ) ),
'tags' => array_map( 'esc_js', apply_filters( 'sranalytics_tags', $tags ) ),
'authors' => array_map( 'esc_js', apply_filters( 'sranalytics_authors', $authors ) ),
);
// Get the JS ready to go
if ($sranalytics_show_beacon) {
wp_register_script( 'sranalytics', plugins_url( 'javascripts/sranalytics.js', __FILE__) );
wp_localize_script( 'sranalytics', 'sranalytics', $javascript_array );
wp_enqueue_script( 'sranalytics' );
} else {
return false;
}
}
/**
* Add the SimpleReach admin section
*/
function sranalytics_load_admin() {
include_once( 'sranalytics_admin.php' );
}
/**
* Add the SimpleReach admin options to the Settings Menu
*/
function sranalytics_admin_actions() {
add_options_page("SimpleReach Analytics", "SimpleReach Analytics", "manage_options", "SimpleReach-Analytics", "sranalytics_load_admin");
}
/**
* Setup the locales for i18n
*/
function sranalytics_textdomain() {
$locale = apply_filters( 'sranalytics_locale', get_locale() );
$mofile = sprintf( 'sranalytics-%s.mo', $locale );
$mofile_local = plugin_dir_path( __FILE__ ) . 'languages/' . $mofile;
return load_textdomain( 'sranalytics', $mofile_local );
}
// Determine when specific methods are supposed to fire
add_action( 'wp_head', 'sranalytics_insert_js', 5 );
add_action( 'admin_menu','sranalytics_admin_actions' );
add_action( 'plugins_loaded', 'sranalytics_textdomain' );