-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwatermanagement.php
More file actions
323 lines (277 loc) · 9.59 KB
/
watermanagement.php
File metadata and controls
323 lines (277 loc) · 9.59 KB
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
/**
* watermanagement PARETO Water Management Plugin
*
* @package WordPress Plugin
* @author Troy Web Consulting
* @license GPL-2.0-or-later
*
* @wordpress-plugin
* Plugin Name: PARETO Water Management
* Plugin URI: https://troyweb.com
* Description: Plugin recreates the basic features of <strong>share.producedwater.org</strong>, a produced water management prototype developed as part of a collaboration between the <strong>Ground Water Protection Council (watersharing)</strong> and the <strong>US Department of Energy (DOE)</strong>. Water management was created to collect information about produced water availability and needs from users, and suggests mutually beneficial trades that minimize transportation distances between users.
* Version: 0.2.1
* Author: Troy Web Consulting
* Author URI: https://troyweb.com
* License: GPL v2 or later
*/
// register plugin hook
register_activation_hook(__FILE__, 'watersharing_plugin_activate');
function watersharing_plugin_activate()
{
flush_rewrite_rules();
}
// deregister plugin hook
register_deactivation_hook(__FILE__, 'watersharing_plugin_deactivate');
function watersharing_plugin_deactivate()
{
flush_rewrite_rules();
}
//define wordpre plugin directory path
define('WATERSHARING_PLUGIN_PATH', plugin_dir_path(__FILE__));
// enqueue plugin styles and scripts
function watersharing_plugin_enqueue()
{
wp_enqueue_style('fontawesome', 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css', array(), false );
wp_enqueue_style('watersharing-styles', plugins_url('assets/dist/css/watersharing.min.css', __FILE__), array(), filemtime(plugin_dir_path(__FILE__) . 'assets/dist/css/watersharing.min.css') );
wp_enqueue_style('datatables-styles', 'https://cdn.datatables.net/v/bs5/dt-1.13.4/datatables.min.css', array(), null);
wp_enqueue_script('jquery');
wp_enqueue_script('datatables-js', 'https://cdn.datatables.net/v/bs5/dt-1.13.4/datatables.min.js', array('jquery'), null, false);
wp_enqueue_script('watersharing-scripts', plugins_url('assets/dist/js/watersharing.min.js', __FILE__), array('jquery'), null, false);
wp_enqueue_script('tablesort', plugins_url('assets/dist/libs/tablesort.js', __FILE__), array('jquery'), null, false);
}
add_action('wp_enqueue_scripts', 'watersharing_plugin_enqueue');
// enqueue the styles and scripts for WP Admin
function watersharing_admin_enqueue( $hook ) {
wp_enqueue_style('watersharing-styles', plugins_url('assets/dist/css/watersharing-admin.min.css', __FILE__), array(), filemtime(plugin_dir_path(__FILE__) . 'assets/dist/css/watersharing.min.css') );
}
add_action( 'admin_enqueue_scripts', 'watersharing_admin_enqueue' );
// setup the sharing settings page
function watersharing_menu() {
require_once( plugin_dir_path( __FILE__ ) . 'inc/watersharing-settings.php' );
add_menu_page(
'Watersharing',
'Watersharing',
'manage_options',
'watersharing-settings',
'watersharing_settings_page',
'dashicons-location',
6
);
add_submenu_page(
'watersharing-settings',
'Watersharing Settings',
'Settings',
'manage_options',
'watersharing-settings',
);
add_submenu_page(
'watersharing-settings',
'Production (Have Water) Requests',
'Production',
'manage_options',
'edit.php?post_type=share_supply',
);
add_submenu_page(
'watersharing-settings',
'Consumption (Need Water) Requests',
'Consumption',
'manage_options',
'edit.php?post_type=share_demand',
);
add_submenu_page(
'watersharing-settings',
'Match Lookup',
'Match Lookup',
'manage_options',
'edit.php?post_type=matched_shares',
);
}
// setup the trading settings page
function watertrading_menu() {
require_once( plugin_dir_path( __FILE__ ) . 'inc/watertrading-settings.php' );
add_menu_page(
'Watertrading',
'Watertrading',
'manage_options',
'watertrading-settings',
'watertrading_settings_page',
'dashicons-location',
7
);
add_submenu_page(
'watertrading-settings',
'Watertrading Settings',
'Settings',
'manage_options',
'watertrading-settings',
);
add_submenu_page(
'watertrading-settings',
'Production (Have Water) Requests',
'Production',
'manage_options',
'edit.php?post_type=trade_supply',
);
add_submenu_page(
'watertrading-settings',
'Consumption (Need Water) Requests',
'Consumption',
'manage_options',
'edit.php?post_type=trade_demand',
);
add_submenu_page(
'watertrading-settings',
'Match Lookup',
'Match Lookup',
'manage_options',
'edit.php?post_type=matched_trades',
);
}
// Water Management Menu - Contains Settings with Trading / Sharing Toggle
function watermanagement_menu() {
require_once( plugin_dir_path( __FILE__ ) . 'inc/watermanagement-settings.php' );
add_menu_page(
'PARETO Water Management',
'PARETO Water Management',
'manage_options',
'watermanagement-settings',
'watermanagement_settings_page',
'dashicons-location',
5
);
add_submenu_page(
'watermanagement-settings',
'Watermanagement Settings',
'Settings',
'manage_options',
'watermanagement-settings',
);
add_submenu_page(
'watermanagement-settings',
'Well Pads',
'Well Pads',
'manage_options',
'edit.php?post_type=well_pad',
);
}
$watersharing_toggle = get_option('watersharing_toggle');
if($watersharing_toggle){
add_action( 'admin_menu', 'watersharing_menu' );
}
$watertrading_toggle = get_option('watertrading_toggle');
if($watertrading_toggle){
add_action( 'admin_menu', 'watertrading_menu' );
}
add_action( 'admin_menu', 'watermanagement_menu' );
// require plugin files
require_once( 'inc/types-taxonomies.php' );
require_once( 'inc/element-builders.php' );
require_once( 'inc/dashboard-ajax.php' );
require_once( 'inc/watersharing-functions.php' );
require_once( 'inc/watersharing-settings.php' );
require_once( 'inc/watersharing-json-exporter.php' );
// define custom block categories
function register_ws_guttenberg_categories( $ws_categories ) {
$ws_categories[] = array(
'slug' => 'watersharing',
'title' => 'Watersharing Blocks',
);
return $ws_categories;
}
function register_wt_guttenberg_categories( $wt_categories ) {
$wt_categories[] = array(
'slug' => 'watertrading',
'title' => 'Watertrading Blocks'
);
return $wt_categories;
}
if ( version_compare( get_bloginfo( 'version' ), '5.8', '>=' ) ) {
add_filter( 'block_categories_all', 'register_ws_guttenberg_categories' );
add_filter( 'block_categories_all', 'register_wt_guttenberg_categories' );
} else {
add_filter( 'block_categories', 'register_ws_guttenberg_categories' );
add_filter( 'block_categories', 'register_wt_guttenberg_categories' );
}
// register ws blocks
function register_watersharing_blocks()
{
$blocks_dir = __DIR__ . '/blocks/build/ws-';
$block_directories = array_filter( glob( $blocks_dir . '*' ), 'is_dir' );
foreach ( $block_directories as $block_dir ) {
register_block_type( $block_dir );
}
}
//register wt blocks
function register_watertrading_blocks()
{
$blocks_dir = __DIR__ . '/blocks/build/wt-';
$block_directories = array_filter( glob( $blocks_dir . '*' ), 'is_dir' );
foreach ( $block_directories as $block_dir ) {
register_block_type( $block_dir );
}
}
if($watersharing_toggle){
add_action('init', 'register_watersharing_blocks');
}
if($watertrading_toggle){
add_action('init', 'register_watertrading_blocks');
}
// handle request dashboard post updates
function change_post_status_callback() {
// Sanitize and validate inputs
$post_ids = [];
if ( isset($_POST['post_ids']) && is_array($_POST['post_ids']) ) {
$post_ids = array_filter( array_map('absint', $_POST['post_ids']) );
}
$post_action = isset($_POST['post_action']) ? sanitize_key($_POST['post_action']) : '';
if ( $post_ids && in_array( $post_action, ['delete', 'close'], true ) ) {
foreach ( $post_ids as $post_id ) {
if ( $post_action === 'delete' ) {
wp_trash_post( $post_id );
} elseif ( $post_action === 'close' ) {
update_post_meta( $post_id, 'status', 'closed' );
}
}
}
// Determine a safe, mode-correct redirect destination
$redirect_url = '';
if ( isset($_POST['redirect_success']) && !empty($_POST['redirect_success']) ) {
$redirect_path = wp_parse_url( sanitize_text_field($_POST['redirect_success']), PHP_URL_PATH );
$redirect_path = ltrim( (string) $redirect_path, '/' );
$redirect_url = home_url( $redirect_path ? '/' . $redirect_path : '/' );
} elseif ( wp_get_referer() ) {
$redirect_url = wp_get_referer();
} elseif ( ! empty($post_ids) ) {
$first_type = get_post_type( $post_ids[0] );
if ( function_exists('ws_dashboard_url_for_post_type') ) {
$redirect_url = ws_dashboard_url_for_post_type( $first_type );
} else {
$redirect_url = home_url('/');
}
} else {
$mode = function_exists('ws_infer_mode') ? ws_infer_mode() : '';
if ( function_exists('ws_dashboard_url_for_mode') ) {
$redirect_url = ws_dashboard_url_for_mode( $mode );
} else {
$redirect_url = home_url('/');
}
}
// Remember mode briefly to guide any mirrored admin-post hits
if ( ! empty($post_ids) ) {
$first_type = isset($first_type) ? $first_type : get_post_type( $post_ids[0] );
if ( is_string($first_type) && $first_type !== '' ) {
$mode = (strpos($first_type, 'trade_') === 0) ? 'watertrading' : 'watersharing';
@setcookie('ws_last_mode', $mode, time() + 300, '/');
}
}
// Redirect with robust fallbacks
if ( function_exists('ws_send_redirect') ) {
ws_send_redirect( $redirect_url, 303 );
}
// Fallback if helper is unavailable
wp_safe_redirect( $redirect_url ? $redirect_url : home_url('/') );
exit;
}
add_action('admin_post_change_post_status', 'change_post_status_callback');
add_action('admin_post_nopriv_change_post_status', 'change_post_status_callback');