PluginProbe
Posts and Users Stats / 1.1.4
Posts and Users Stats v1.1.4
trunk 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5
posts-and-users-stats / posts-and-users-stats.php

posts-and-users-stats.php in Posts and Users Stats 1.1.4, at posts-and-users-stats.php

201 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Posts and Users Stats
4 * Plugin URI: https://patrick-robrecht.de/wordpress/
5 * Description: Statistics about the number of posts and users, provided as diagrams, tables and csv export.
6 * Version: 1.1.4
7 * Author: Patrick Robrecht
8 * Author URI: https://patrick-robrecht.de/
9 * License: GPLv3
10 * License URI: https://www.gnu.org/licenses/gpl-3.0.html
11 * Text Domain: posts-and-users-stats
12 *
13 * @package posts-and-users-stats
14 */
15
16 // Exit if accessed directly.
17 defined( 'ABSPATH' ) || exit;
18
19 define( 'POSTS_AND_USERS_STATS_VERSION', '1.1.4' );
20
21 /**
22 * Load text domain for translation.
23 */
24 function posts_and_users_stats_load_plugin_textdomain() {
25 load_plugin_textdomain( 'posts-and-users-stats' );
26 }
27 // Add text domain during initialization.
28 add_action( 'init', 'posts_and_users_stats_load_plugin_textdomain' );
29
30 /**
31 * Load CSS and JavaScript libraries.
32 */
33 function posts_and_users_stats_load_assets() {
34 if ( posts_and_users_stats_current_user_can() ) {
35 posts_and_users_stats_enqueue_style(
36 'posts-and-users-stats-css',
37 '/assets/style.min.css'
38 );
39 posts_and_users_stats_enqueue_style(
40 'chartist-css',
41 '/lib/chartist.min.css'
42 );
43
44 posts_and_users_stats_enqueue_script(
45 'chartist',
46 '/lib/chartist.min.js'
47 );
48 posts_and_users_stats_enqueue_script(
49 'chartist-plugin-axistitle',
50 '/lib/chartist-plugin-axistitle.min.js'
51 );
52 posts_and_users_stats_enqueue_script(
53 'moment',
54 '/lib/moment.min.js'
55 );
56 posts_and_users_stats_enqueue_script(
57 'posts-and-users-stats-functions',
58 '/assets/functions.min.js'
59 );
60 }
61 }
62
63 /**
64 * Loads the CSS file.
65 *
66 * @param string $style_name the name of the style.
67 * @param string $style_path the plugin-relative path of the CSS file.
68 */
69 function posts_and_users_stats_enqueue_style( $style_name, $style_path ) {
70 wp_enqueue_style(
71 $style_name,
72 plugins_url(
73 $style_path,
74 __FILE__
75 ),
76 [],
77 POSTS_AND_USERS_STATS_VERSION
78 );
79 }
80
81 /**
82 * Loads the JavaScript file.
83 *
84 * @param string $script_name the name of the script.
85 * @param string $script_path the plugin-relative path of the JavaScript.
86 * @param array $dependencies the dependencies.
87 */
88 function posts_and_users_stats_enqueue_script( $script_name, $script_path, $dependencies = [] ) {
89 wp_enqueue_script(
90 $script_name,
91 plugins_url(
92 $script_path,
93 __FILE__
94 ),
95 $dependencies,
96 POSTS_AND_USERS_STATS_VERSION
97 );
98 }
99
100 /**
101 * Create an item and submenu items in the WordPress admin menu.
102 */
103 function posts_and_users_stats_add_menu() {
104 $page_hook_suffixes = array();
105 $page_hook_suffixes[] = add_management_page(
106 __( 'Posts Statistics', 'posts-and-users-stats' ),
107 __( 'Posts Statistics', 'posts-and-users-stats' ),
108 'export',
109 'posts_and_users_stats_posts',
110 'posts_and_users_stats_show_posts'
111 );
112 $page_hook_suffixes[] = add_management_page(
113 __( 'Comments Statistics', 'posts-and-users-stats' ),
114 __( 'Comments Statistics', 'posts-and-users-stats' ),
115 'export',
116 'posts_and_users_stats_comments',
117 'posts_and_users_stats_show_comments'
118 );
119 $page_hook_suffixes[] = add_management_page(
120 __( 'Users Statistics', 'posts-and-users-stats' ),
121 __( 'Users Statistics', 'posts-and-users-stats' ),
122 'export',
123 'posts_and_users_stats_users',
124 'posts_and_users_stats_show_users'
125 );
126
127 // Load CSS and JavaScript on plugin pages.
128 foreach ( $page_hook_suffixes as $page_hook_suffix ) {
129 add_action( "admin_print_styles-{$page_hook_suffix}", 'posts_and_users_stats_load_assets' );
130 }
131 }
132 // Register the menu building function.
133 add_action( 'admin_menu', 'posts_and_users_stats_add_menu' );
134
135 /**
136 * Checks whether the current user is in the admin area and has the capability to see the pages.
137 *
138 * @return true if and only if the current user is allowed to see plugin pages
139 */
140 function posts_and_users_stats_current_user_can() {
141 return is_admin() && current_user_can( 'export' );
142 }
143
144 /**
145 * Show the posts stats page.
146 */
147 function posts_and_users_stats_show_posts() {
148 if ( posts_and_users_stats_current_user_can() ) {
149 include_once 'views/posts.php';
150 }
151 }
152
153 /**
154 * Show the comments stats page.
155 */
156 function posts_and_users_stats_show_comments() {
157 if ( posts_and_users_stats_current_user_can() ) {
158 include_once 'views/comments.php';
159 }
160 }
161
162 /**
163 * Show the users stats page.
164 */
165 function posts_and_users_stats_show_users() {
166 if ( posts_and_users_stats_current_user_can() ) {
167 include_once 'views/users.php';
168 }
169 }
170
171 /**
172 * Echo the class attribute of a navigation tab.
173 *
174 * @param bool $is_active_tab true if and only if the tab is active.
175 */
176 function posts_and_users_stats_echo_tab_class( $is_active_tab ) {
177 echo 'nav-tab';
178 if ( $is_active_tab ) {
179 echo ' nav-tab-active';
180 }
181 }
182
183 /**
184 * Output the link to a csv export.
185 *
186 * @param string $button_id the ID of the link.
187 * @param string $table_id the ID of the table to export.
188 * @param string $name the file name.
189 */
190 function posts_and_users_stats_echo_export_button( $button_id, $table_id, $name ) {
191 $filename = str_replace( ' ', '-', get_bloginfo( 'name' ) . '-' . $name ) . '-' . gmdate( 'Y-m-d-H-i-s' ); ?>
192 <a class="page-title-action" href="#" id="<?php echo esc_attr( $button_id ); ?>" role="button"><?php esc_html_e( 'Export as CSV', 'posts-and-users-stats' ); ?></a>
193 <script type='text/javascript'>
194 jQuery(document).ready(function () {
195 jQuery("#<?php echo esc_attr( $button_id ); ?>").click(function (event) {
196 posts_and_users_stats_export_table_to_csv.apply(this, [jQuery('#<?php echo esc_attr( $table_id ); ?>'), '<?php echo esc_attr( $filename ); ?>.csv']);
197 });
198 });
199 </script>
200 <?php } ?>
201