-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevolution-google-analytics-code.php
190 lines (166 loc) · 5.53 KB
/
evolution-google-analytics-code.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
<?php
/*
Plugin Name: AH Google Analytics Code
Plugin URI: https://wordpress.org/plugins/evolution-google-analytics-code/developers/
Description: With this plugin you can add the Google Analytics Code (or any other) in no time to your header or footer.
Version: 1.0.7
Author: Andreas Hecht
Author URI: https://andreas-hecht.com/
License: GPL2 or higher
License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
class EvolutionAnalyticsSettingsPage
{
/**
* Holds the values to be used in the fields callbacks
*/
private $options;
/**
* Start up
*/
public function __construct()
{
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'page_init' ) );
}
/**
* Add options page
*/
public function add_plugin_page()
{
// This page will be under "Settings"
add_options_page(
'AH Google Analytics Settings',
'AH Google Analytics',
'manage_options',
'evolution-google-analytics',
array( $this, 'create_admin_page' )
);
}
/**
* Options page callback
*/
public function create_admin_page()
{
// Set class property
$this->options = get_option( 'analytics_options' );
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2>AH Google Analytics Code</h2>
<div id="poststuff" class="metabox-holder has-right-sidebar">
<div class="container postbox postbox-container" style="width: 60%">
<h3 class="hndle">Settings</h3>
<div class="inside">
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'analytics_group' );
do_settings_sections( 'my-setting-admin' );
submit_button();
?>
</form>
</div>
</div>
</div>
<?php
}
/**
* Register and add settings
*/
public function page_init()
{
register_setting(
'analytics_group', // Option group
'analytics_options', // Option name
array( $this, 'sanitize' ) // Sanitize
);
add_settings_section(
'setting_section_id', // ID
'Paste your Google Analytics Code in one of the textareas below', // Title
array( $this, 'print_section_info' ), // Callback
'my-setting-admin' // Page
);
add_settings_field(
'evolution_analytics_head', // ID
'Add your Google Analytics Code to header: wp_head();', // Title
array( $this, 'evolution_analytics_head_callback' ), // Callback
'my-setting-admin', // Page
'setting_section_id' // Section
);
add_settings_field(
'evolution_analytics_footer',
'Add your Google Analytics Code to footer: wp_footer();',
array( $this, 'evolution_analytics_footer_callback' ),
'my-setting-admin',
'setting_section_id'
);
}
/**
* Sanitize each setting field as needed
*
* @param array $input Contains all settings fields as array keys
*/
public function sanitize( $input )
{
$new_input = array();
if( isset( $input['evolution_analytics_head'] ) )
$new_input['evolution_analytics_head'] = $input['evolution_analytics_head'];
if( isset( $input['evolution_analytics_footer'] ) )
$new_input['evolution_analytics_footer'] = $input['evolution_analytics_footer'];
return $new_input;
}
/**
* Print the Section text
*/
public function print_section_info()
{
print ''; // Nothing to say here
}
/**
* Get the settings option array and print one of its values
*/
public function evolution_analytics_head_callback()
{
printf(
'<textarea style="width: 350px; height: 150px;" type="text" id="evolution_analytics_head" name="analytics_options[evolution_analytics_head]">%s</textarea>',
isset( $this->options['evolution_analytics_head'] ) ? esc_attr( $this->options['evolution_analytics_head']) : ''
);
}
/**
* Get the settings option array and print one of its values
*/
public function evolution_analytics_footer_callback()
{
printf(
'<textarea style="width: 350px; height: 150px;" type="text" id="evolution_analytics_footer" name="analytics_options[evolution_analytics_footer]">%s</textarea>',
isset( $this->options['evolution_analytics_footer'] ) ? esc_attr( $this->options['evolution_analytics_footer']) : ''
);
}
}
if( is_admin() )
$my_settings_page = new EvolutionAnalyticsSettingsPage();
/**
*
* Add analytics code to wp_head()
* @uses wp_head()
*
*/
function evolution_print_analytics_code_header()
{
$my_options = get_option( 'analytics_options' );
echo $my_options['evolution_analytics_head'];
}
add_action( 'wp_head', 'evolution_print_analytics_code_header' );
/**
*
* Add analytics code to wp_footer()
* @uses wp_footer()
*
*/
function evolution_print_analytics_code_footer()
{
$my_options = get_option( 'analytics_options' );
echo $my_options['evolution_analytics_footer'];
}
add_action( 'wp_footer', 'evolution_print_analytics_code_footer' );