-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextended-evaluation-for-statify.php
290 lines (262 loc) · 7.52 KB
/
extended-evaluation-for-statify.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
/**
* Plugin Name: Statify – Extended Evaluation
* Plugin URI: https://patrick-robrecht.de/wordpress/
* Description: Extended evaluation for the compact, easy-to-use and privacy-compliant Statify plugin.
* Version: 2.6.4
* Author: Patrick Robrecht
* Author URI: https://patrick-robrecht.de/
* License: GPLv3
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
* Text Domain: extended-evaluation-for-statify
*
* @package extended-evaluation-for-statify
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
define( 'EEFSTATFIFY_VERSION', '2.6.4' );
// Includes.
require_once 'inc/queries.php';
require_once 'inc/formatting.php';
/**
* Requires Statify to be installed and activated during installation.
*/
function eefstatify_activate() {
if ( ! eefstatify_is_statify_active() ) {
deactivate_plugins( __FILE__ );
wp_die(
esc_html(
__(
'Statify – Extended Evaluation requires the plugin Statify which has to be installed and activated! Please install and activate Statify before activating this plugin!',
'extended-evaluation-for-statify'
)
),
esc_html(
__(
'Activation Error: Statify – Extended Evaluation requires Statify!',
'extended-evaluation-for-statify'
)
),
array(
'response' => 200,
'back_link' => true,
)
);
}
}
// Hook to run during plugin activation.
register_activation_hook( __FILE__, 'eefstatify_activate' );
/**
* Delete database entries created by the plugin on uninstall.
*/
function eefstatify_uninstall() {
// Nothing to do.
}
// Hook to run during plugin uninstall.
register_uninstall_hook( __FILE__, 'eefstatify_uninstall' );
/**
* Check for Statify plugin being installed.
* If it isn't, display message and deactivate this plugin.
*/
function eefstatify_check_status() {
if ( ! eefstatify_is_statify_active() ) {
// Display warning in the admin area.
echo '<div class="error"><p>'
. esc_html(
__(
'Statify – Extended Evaluation requires the plugin Statify which has to be installed and activated! Please install and activate Statify before activating this plugin!',
'extended-evaluation-for-statify'
)
)
. '</p></div>';
// Deactivate this plugin.
deactivate_plugins( __FILE__ );
}
}
// Add status check to the admin notices.
add_action( 'admin_notices', 'eefstatify_check_status' );
/**
* Test whether Statify is active.
*
* @return boolean true if and only if Statify is installed and active.
*/
function eefstatify_is_statify_active() {
return is_plugin_active( 'statify/statify.php' );
}
/**
* Load text domain for translation.
*/
function eefstatify_load_plugin_textdomain() {
load_plugin_textdomain( 'extended-evaluation-for-statify' );
}
// Add text domain during initialization.
add_action( 'init', 'eefstatify_load_plugin_textdomain' );
/**
* Register and load the style sheets and JavaScript libraries.
*/
function eefstatify_register_and_load_assets() {
if ( eefstatify_current_user_can_see_evaluation() ) {
eefstatify_enqueue_style(
'chartist',
'/lib/chartist.min.css'
);
eefstatify_enqueue_style(
'chartist-plugin-tooltip',
'lib/chartist-plugin-tooltip.min.css'
);
eefstatify_enqueue_style(
'eefstatify',
'/css/style.min.css'
);
eefstatify_enqueue_script(
'chartist',
'/lib/chartist.min.js'
);
eefstatify_enqueue_script(
'chartist-plugin-tooltip',
'lib/chartist-plugin-tooltip.min.js'
);
eefstatify_enqueue_script(
'eefstatify_functions',
'/js/functions.min.js',
[ 'chartist', 'chartist-plugin-tooltip', 'jquery' ]
);
wp_localize_script(
'eefstatify_functions',
'eefStatifyTranslations',
array(
'view' => strip_tags( esc_html__( 'View', 'extended-evaluation-for-statify' ) ),
'views' => strip_tags( esc_html__( 'Views', 'extended-evaluation-for-statify' ) ),
)
);
}
}
/**
* Loads the CSS file.
*
* @param string $style_name the name of the style.
* @param string $style_path the plugin-relative path of the CSS file.
*/
function eefstatify_enqueue_style( $style_name, $style_path ) {
wp_enqueue_style(
$style_name,
plugins_url(
$style_path,
__FILE__
),
[],
EEFSTATFIFY_VERSION
);
}
/**
* Loads the JavaScript file.
*
* @param string $script_name the name of the script.
* @param string $script_path the plugin-relative path of the JavaScript.
* @param array $dependencies the dependencies.
*/
function eefstatify_enqueue_script( $script_name, $script_path, $dependencies = [] ) {
wp_enqueue_script(
$script_name,
plugins_url(
$script_path,
__FILE__
),
$dependencies,
EEFSTATFIFY_VERSION
);
}
/**
* Create an item and submenu items in the WordPress admin menu.
*/
function eefstatify_add_menu() {
$page_hook_suffixes = [];
$page_hook_suffixes[] = add_menu_page(
__( 'Statify – Extended Evaluation', 'extended-evaluation-for-statify' ), // page title.
'Statify', // title in the menu.
'see_statify_evaluation',
'extended_evaluation_for_statify_dashboard',
'eefstatify_show_dashboard',
'dashicons-chart-bar',
50
);
$page_hook_suffixes[] = add_submenu_page(
'extended_evaluation_for_statify_dashboard',
__( 'Content', 'extended-evaluation-for-statify' )
. ' — ' . __( 'Statify – Extended Evaluation', 'extended-evaluation-for-statify' ),
__( 'Content', 'extended-evaluation-for-statify' ),
'see_statify_evaluation',
'extended_evaluation_for_statify_content',
'eefstatify_show_content'
);
$page_hook_suffixes[] = add_submenu_page(
'extended_evaluation_for_statify_dashboard',
__( 'Referrers', 'extended-evaluation-for-statify' )
. ' — ' . __( 'Statify – Extended Evaluation', 'extended-evaluation-for-statify' ),
__( 'Referrers', 'extended-evaluation-for-statify' ),
'see_statify_evaluation',
'extended_evaluation_for_statify_referrer',
'eefstatify_show_referrer'
);
// Load CSS and JavaScript on plugin pages.
foreach ( $page_hook_suffixes as $page_hook_suffix ) {
add_action( "admin_print_styles-{$page_hook_suffix}", 'eefstatify_register_and_load_assets' );
}
}
// Register the menu building function.
add_action( 'admin_menu', 'eefstatify_add_menu' );
/**
* Adds a custom capability for users who see the evaluation pages.
*/
function eefstatify_add_capability() {
// Only administrators can see the evaluation by default.
$role = get_role( 'administrator' );
if ( $role ) {
$role->add_cap( 'see_statify_evaluation' );
}
// Remove old role Statify Analyst.
$role = get_role( 'statify_evaluator' );
if ( $role ) {
remove_role( 'statify_evaluator' );
}
}
// Adds the capability.
add_action( 'admin_init', 'eefstatify_add_capability' );
/**
* Checks whether the current user is in the admin area and has the capability to see the evaluation.
*
* @return true if and only if the current user is allowed to see plugin pages
*/
function eefstatify_current_user_can_see_evaluation() {
return is_admin() && current_user_can( 'see_statify_evaluation' );
}
/**
* Show the dashboard page.
*/
function eefstatify_show_dashboard() {
eefstatify_load_view( __DIR__ . '/views/dashboard.php' );
}
/**
* Show the most popular content statistics page.
*/
function eefstatify_show_content() {
eefstatify_load_view( __DIR__ . '/views/content.php' );
}
/**
* Show the referrer statistics page.
*/
function eefstatify_show_referrer() {
eefstatify_load_view( __DIR__ . '/views/referrers.php' );
}
/**
* Loads the view template.
*
* @param string $view_path the path to the view template.
*/
function eefstatify_load_view( $view_path ) {
if ( eefstatify_current_user_can_see_evaluation() ) {
load_template(
wp_normalize_path( $view_path )
);
}
}