-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathB3AssetsTracker.php
268 lines (228 loc) · 12.8 KB
/
B3AssetsTracker.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
<?php
/*
Plugin Name: B3 : Assets Tracker
Description: This plugin gives you the option to track and analyze your (financial) assets.
Version: 1.15.0
Author: Beee
Author URI: https://berryplasman.com
License: GPL2
License: GPL v2 (or later)
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: b3-assets-tracker
Domain Path: /languages
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'B3AssetsTracker' ) ) :
/**
* Main class
*/
class B3AssetsTracker {
/**
* A dummy constructor to ensure plugin is only initialized once
*/
public function __construct() {}
public function initialize() {
register_activation_hook( __FILE__, [ $this, 'bp_plugin_activation' ] );
register_deactivation_hook( __FILE__, [ $this, 'bp_plugin_deactivation' ] );
add_action( 'admin_init', [ $this, 'bp_check_table' ] );
add_action( 'admin_menu', [ $this, 'bp_admin_pages' ] );
add_action( 'wp_enqueue_scripts', [ $this, 'bp_add_css_front' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'bp_add_css_admin' ] );
add_action( 'init', [ $this, 'bp_load_textdomain' ] );
include 'actions.php';
include 'data.php';
include 'functions.php';
}
/**
* Function which runs upon plugin activation
*/
public function bp_plugin_activation() {
$this->bp_check_table();
update_option( 'bp_currency', '€' );
update_option( 'bp_date_format', 'd-m-y' );
global $wpdb;
$table = $wpdb->prefix . 'asset_groups';
$results = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM %i", $table ) );
if ( empty( $results ) ) {
foreach( b3_get_default_groups() as $id => $name ) {
$data = [ 'id' => $id, 'name' => $name ];
$wpdb->insert( $table, $data, [ '%d', '%s' ] );
}
}
}
/**
* Function which runs upon plugin deactivation
*/
public function bp_plugin_deactivation() {
delete_option( 'bp_date_format' );
delete_option( 'bp_currency' );
}
/**
* Add admin page
*/
public function bp_settings() {
if ( ! is_admin() ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
return [
'db_version' => '1.3',
'version' => get_plugin_data( __FILE__ )['Version'],
];
}
public function bp_load_textdomain() {
load_plugin_textdomain( 'b3-assets-tracker', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
/**
* Add admin page
*/
public function bp_admin_pages() {
include 'admin/dashboard.php';
add_menu_page( 'A$$et$', 'A$$et$', 'manage_options', 'bp-assets-dashboard', 'bp_assets_dashboard', 'dashicons-chart-pie', '3' );
include 'admin/data.php';
add_submenu_page( 'options.php', esc_html__( 'Data', 'b3-assets-tracker' ), esc_html__( 'Data', 'b3-assets-tracker' ), 'manage_options', 'bp-assets-data', 'bp_assets_data' );
include 'admin/add-data.php';
add_submenu_page( 'options.php', esc_html__( 'Add data' ), esc_html__( 'Add data' ), 'manage_options', 'bp-assets-add-data', 'bp_assets_add_data' );
include 'admin/add-type.php';
add_submenu_page( 'options.php', esc_html__( 'Types', 'b3-assets-tracker' ), esc_html__( 'Types', 'b3-assets-tracker' ), 'manage_options', 'bp-assets-types', 'bp_assets_add_type' );
include 'admin/graphs.php';
add_submenu_page( 'options.php', esc_html__( 'Graphs', 'b3-assets-tracker' ), esc_html__( 'Graphs', 'b3-assets-tracker' ), 'manage_options', 'bp-assets-graphs', 'bp_assets_graphs' );
include 'admin/settings.php';
add_submenu_page( 'options.php', esc_html__( 'Settings', 'b3-assets-tracker' ), esc_html__( 'Settings', 'b3-assets-tracker' ), 'manage_options', 'bp-assets-settings', 'bp_assets_settings' );
}
/**
* Add css
*/
public function bp_add_css_admin() {
wp_register_style( 'bp-assets-admin', plugins_url( 'assets/admin.css', __FILE__ ), [], $this->bp_settings()[ 'version' ] );
wp_enqueue_style( 'bp-assets-admin' );
wp_enqueue_script( 'charts', plugins_url( 'assets/js.js', __FILE__ ), [], $this->bp_settings()[ 'version' ], false );
wp_enqueue_script( 'graphs', plugins_url( 'assets/graphs.js', __FILE__ ), [ 'jquery' ], $this->bp_settings()[ 'version' ], true );
if ( isset( $_POST[ 'b3_from_till_nonce' ] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST[ 'b3_from_till_nonce' ] ) ), 'b3-from-till-nonce' ) ) {
if ( isset( $_POST[ 'show_graph' ] ) ) {
$validated = b3_validate_graph_fields( $_POST );
if ( $validated ) {
$asset_types = isset( $_POST[ 'asset_type' ] ) ? wp_unslash( $_POST[ 'asset_type' ] ) : [];
$asset_types = in_array( 'all', $asset_types ) ? 'all' : $asset_types;
$asset_groups = isset( $_POST[ 'asset_group' ] ) ? wp_unslash( $_POST[ 'asset_group' ] ) : [];
$asset_groups = in_array( 'all', $asset_groups ) ? 'all' : $asset_groups;
$date_from = isset( $_POST[ 'stats_from' ] ) ? sanitize_text_field( wp_unslash( $_POST[ 'stats_from' ] ) ) : '';
$date_until = isset( $_POST[ 'stats_until' ] ) ? sanitize_text_field( wp_unslash( $_POST[ 'stats_until' ] ) ) : '';
$dates = isset( $_POST[ 'bp_dates' ] ) ? wp_unslash( $_POST[ 'bp_dates' ] ) : [];
$graph_type = isset( $_POST[ 'graph_type' ] ) ? sanitize_text_field( wp_unslash( $_POST[ 'graph_type' ] ) ) : '';
$show_all = 'all' == $asset_types || 'all' == $asset_groups ? true : false;
$grouped_data = bp_get_results_range( $dates, $asset_types, $asset_groups, $show_all );
$h_axis_title = esc_html__( 'Date', 'b3-assets-tracker' );
$v_axis_title = esc_html__( 'Value', 'b3-assets-tracker' );
if ( ! empty( $grouped_data ) ) {
$processed_data = bp_process_data_for_chart( $grouped_data, $asset_types, $asset_groups, $graph_type );
if ( 'bar' === $graph_type ) {
$h_axis_title = esc_html__( 'Value', 'b3-assets-tracker' );
$v_axis_title = esc_html__( 'Asset', 'b3-assets-tracker' );
}
$graph_title_args = [
'type' => $graph_type,
'asset_type' => $asset_types,
];
$graph_title = bp_get_graph_title( $graph_title_args );
$margin_top = 'auto';
$margin_left = 'auto';
$margin_right = 'auto';
$chart_args = [
'asset_group' => $asset_groups,
'asset_type' => $asset_types,
'currency' => get_option( 'bp_currency' ),
'graph_title' => $graph_title,
'graph_type' => $graph_type,
'h_axis_title' => $h_axis_title,
'v_axis_title' => $v_axis_title,
'legend' => 'right',
'margin_top' => $margin_top,
'margin_left' => $margin_left,
'margin_right' => $margin_right,
'data' => $processed_data,
];
wp_enqueue_script( 'google-chart', 'https://www.gstatic.com/charts/loader.js', [], $this->bp_settings()[ 'version' ], false );
wp_localize_script( 'graphs', 'chart_vars', $chart_args );
}
}
}
}
}
public function bp_add_css_front() {
if ( ! is_admin() ) {
wp_register_style( 'bp-assets-front', plugins_url( 'assets/front.css', __FILE__ ), [], $this->bp_settings()[ 'version' ] );
wp_enqueue_style( 'bp-assets-front' );
// @TODO: add check IF shortcode is used
wp_enqueue_script( 'charts', plugins_url( 'assets/js.js', __FILE__ ), [ 'jquery' ], $this->bp_settings()[ 'version' ], false );
wp_enqueue_script( 'google-chart', 'https://www.gstatic.com/charts/loader.js', [], $this->bp_settings()[ 'version' ], false );
wp_enqueue_script( 'graphs', plugins_url( 'assets/graphs.js', __FILE__ ), [ 'jquery' ], $this->bp_settings()[ 'version' ], true );
}
}
public function bp_check_table() {
$db_version = get_option( 'assets_db_version', false );
if ( false == $db_version || $db_version != $this->bp_settings()[ 'db_version' ] ) {
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
global $wpdb;
ob_start();
?>
CREATE TABLE <?php echo esc_sql( $wpdb->prefix ); ?>asset_types (
id int(6) unsigned NOT NULL auto_increment,
name varchar(50) NOT NULL,
ordering int(2) NOT NULL,
asset_group int(2) NOT NULL,
hide int(1) unsigned NULL,
added DATE NULL,
closed DATE NULL,
PRIMARY KEY (id)
)
COLLATE <?php echo esc_sql( $wpdb->collate ); ?>;
<?php
$sql1 = ob_get_clean();
dbDelta( $sql1 );
ob_start();
?>
CREATE TABLE <?php echo esc_sql( $wpdb->prefix ); ?>asset_data (
id int(6) unsigned NOT NULL auto_increment,
date DATE NOT NULL,
type int(2) NOT NULL,
value decimal(8,2) NOT NULL,
updated int(11) NULL,
PRIMARY KEY (id)
)
COLLATE <?php echo esc_sql( $wpdb->collate ); ?>;
<?php
$sql2 = ob_get_clean();
dbDelta( $sql2 );
ob_start();
?>
CREATE TABLE <?php echo esc_sql( $wpdb->prefix ); ?>asset_groups (
id int(6) unsigned NOT NULL auto_increment,
name varchar(50) NOT NULL,
PRIMARY KEY (id)
)
COLLATE <?php echo esc_sql( $wpdb->collate ); ?>;
<?php
$sql3 = ob_get_clean();
dbDelta( $sql3 );
update_option( 'assets_db_version', $this->bp_settings()[ 'db_version' ] );
}
}
}
/**
* The main function responsible for returning the one true B3AssetsTracker instance to functions everywhere.
*
* @return \B3AssetsTracker
*/
function init_assets_plugin() {
global $assets_plugin;
if ( ! isset( $assets_plugin ) ) {
$assets_plugin = new B3AssetsTracker();
$assets_plugin->initialize();
}
return $assets_plugin;
}
// initialize
init_assets_plugin();
endif;