-
Notifications
You must be signed in to change notification settings - Fork 5
/
wp-magic-crud.php
executable file
·339 lines (276 loc) · 9.3 KB
/
wp-magic-crud.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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
/*
* Plugin Name: PluggableSoft - WP Magic Crud
* Description: Magic admin CRUDs for WordPress
* Version: 1.0
* Author: Moises Heberle
* Author URI: https://pluggablesoft.com/
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
use WPMC\Action\AutoRunScheduler;
use WPMC\Action\BackgroundAction;
use WPMC\Entity\DatabaseCleanup;
use WPMC\Action\ActionLog;
use WPMC\DB\EntityDataTrack;
use WPMC\Entity;
use WPMC\Entity\EntityLoader;
use WPMC\UI\CommonAdmin;
use WPMC\UI\CommonHtml;
use WPMC\UI\FlashMessage;
defined( 'ABSPATH' ) or die( 'Not allowed' );
defined( 'WPMC_ROOT_DIR' ) || define('WPMC_ROOT_DIR', plugin_dir_path( __FILE__ ));
require_once __DIR__ . '/vendor/autoload.php';
register_activation_hook( __FILE__, function(){
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
if ( function_exists('as_unschedule_all_actions') ) {
as_unschedule_all_actions('wpmc_autorun_check');
}
});
add_action('init', function () {
// load ActionScheduler library
require_once __DIR__ . '/vendor/woocommerce/action-scheduler/action-scheduler.php';
}, -1000);
add_action('init', function () {
if ( psNeedScheduleCron() ) {
if ( !as_has_scheduled_action( 'psoft_cruds_sync' ) ) {
as_schedule_recurring_action( time(), ( MINUTE_IN_SECONDS * 15 ), 'psoft_cruds_sync' );
}
}
});
add_action('action_scheduler_before_execute', function(){
static $hasRun = false;
if ( $hasRun ) return;
else $hasRun = true;
//
// add listeners for AutoRun actions pre-dispatch checker
//
$autoRunActions = (new AutoRunScheduler())->getAllAutoRunActions();
foreach ( $autoRunActions as $action ) {
$autoRun = $action->getAutoRun();
add_action($autoRun->getCheckerJobHook(), function() use($action){
$action->getAutoRun()->maybeDispatchAutoRunTasks();
}, 10, 3);
}
//
// add listeners for background actions final execution
//
$backgroundActions = BackgroundAction::getAllBackgroundActions();
foreach ( $backgroundActions as $action )
{
add_action($action->getJobHook(), function($ids = [], $params = []) use($action) {
$runner = $action->getRunner();
$runner->executeNow($ids, $params);
}, 10, 3);
}
//
// add listeners for Database Cleanups tasks
//
$dbCleanups = DatabaseCleanup::getAllDbCleanups();
foreach ( $dbCleanups as $cleanup ) {
add_action($cleanup->getCleanupHook(), function() use($cleanup) {
$cleanup->runEntityCleanup();
}, 10, 2);
}
});
// ActionScheduler
add_action('psoft_cruds_sync', function(){
wpmc_sync_entities();
do_action('psoft_after_cruds_sync');
});
add_action('admin_notices', function(){
\WPMC\UI\FlashMessage::show_flash_messages();
});
// load admin menus for all entities
add_action('admin_menu', function(){
add_menu_page( 'PSoft', 'PSoft', '', 'psoft-manager', null, 'dashicons-admin-multisite' );
$entities = wpmc_load_app_entities();
// trigger entity menus
foreach ( $entities as $entity ) {
$menu = new \WPMC\UI\AdminMenu($entity);
$menu->loadAdminMenu();
}
do_action('wpmc_loaded');
}, 500);
// admin styles
add_action('admin_enqueue_scripts', function(){
wp_enqueue_style('wpmc-styles', plugins_url('/wpmc/styles.css', __FILE__));
wp_enqueue_script('wpmc-scripts', plugins_url('/wpmc/scripts.js', __FILE__));
});
// add this plugin to automatic schema updater
add_filter('psoft_schema_update', function($plugins){
$plugins[] = __FILE__;
return $plugins;
});
add_action('rest_api_init', function (){
foreach ( wpmc_load_app_entities() as $entity ) {
if ( !$entity->getRest()->getExposeAsRest() ) {
continue;
}
$rest = new \WPMC\Rest\EntityRest($entity);
$rest->registerPaginationRest();
$rest->registerGetRest();
$rest->registerPostRest();
$rest->registerPutRest();
$rest->registerActionsRest();
}
});
add_filter('wpmc_load_entities', function($entities){
$entities['action_logs'] = WPMC_ROOT_DIR . '/cruds/action_logs.json';
$entities['data_track'] = WPMC_ROOT_DIR . '/cruds/data_track.json';
return $entities;
});
add_filter('jwt_auth_whitelist', function ( $endpoints ) {
$endpoints = [
'/api/premium/request_update',
'/api/premium/send_message',
'/api/system/test',
];
return array_unique( array_merge( $endpoints, $endpoints ) );
});
if ( !function_exists('wpmc_load_app_entities')) {
/**
* @return \WPMC\Entity[]
*/
function wpmc_load_app_entities() {
$loader = EntityLoader::getELInstance();
return $loader->loadEntityObjects();
}
}
if ( !function_exists('wpmc_get_entity')) {
/**
* @return \WPMC\Entity
*/
function wpmc_get_entity($name) {
$loader = EntityLoader::getELInstance();
return $loader->getEntityByAlias($name);
}
}
if ( !function_exists('wpmc_sync_entities')) {
function wpmc_sync_entities() {
(new \WPMC\EntitySync())->syncEntities();
}
}
if ( !function_exists('wpmc_resync_entities')) {
function wpmc_resync_entities() {
$loader = EntityLoader::getELInstance();
$loader->clearEntitiesCache();
wpmc_sync_entities();
}
}
if ( !function_exists('wpmc_redirect')) {
function wpmc_redirect($url) {
if ( !empty($_GET['admin_back_to']) ) {
$url = get_admin_url(get_current_blog_id(), 'admin.php?' . http_build_query(unserialize(base64_decode($_GET['admin_back_to']))));
}
?>
<script>
window.location.href = "<?php echo $url; ?>";
</script>
<?php
exit;
}
}
if ( !function_exists('wpmc_flash_message') ) {
function wpmc_flash_message($message, $class = null) {
FlashMessage::queue_flash_message($message, $class);
}
}
if ( !function_exists('wpmc_flash_render') ) {
function wpmc_flash_render() {
FlashMessage::show_flash_messages();
}
}
if ( !function_exists('wpmc_entity_admin_url') ) {
function wpmc_entity_admin_url($entity, $filters = []) {
if ( is_string($entity) ) {
$entity = wpmc_get_entity($entity);
}
if ( $entity instanceof Entity ) {
$identifier = $entity->getIdentifier();
return CommonAdmin::adminUrlWithFilters($identifier, $filters);
}
}
}
if ( !function_exists('wpmc_entity_identifier_link')) {
function wpmc_entity_identifier_link($entity) {
$obj = wpmc_get_entity($entity);
if ( $obj->getMenu()->getDisplayMenu() ) {
$url = wpmc_entity_admin_url($entity);
return CommonHtml::htmlLink($url, $entity);
}
else {
return $entity;
}
}
}
if (!function_exists('psTableExists')) {
function psTableExists($table) {
global $wpdb;
return $wpdb->get_var("SHOW TABLES LIKE '{$table}'") == $table;
}
}
if (!function_exists('psTableColumnExists')) {
function psTableColumnExists($table, $column) {
global $wpdb;
$colName = $wpdb->get_var( "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '{$table}' AND column_name = '{$column}'" );
return !empty($colName);
}
}
if ( !function_exists('psNeedScheduleCron')) {
function psNeedScheduleCron() {
if ( !function_exists('as_enqueue_async_action') ) {
return false;
}
return defined('DOING_CRON') || ( preg_match('/crontrol_admin_manage_page/', $_SERVER['REQUEST_URI']) && !defined('DOING_AJAX') );
}
}
if ( !function_exists('convertStdToArray')) {
function convertStdToArray($data) {
return json_decode(json_encode($data), true);
}
}
if ( !function_exists('wpmc_rerun_action')) {
function wpmc_rerun_action(\WPMC\Action\ActionRunner $runner) {
$actionIds = $runner->getContextIds();
$result = [];
foreach ( $actionIds as $id ) {
$log = ActionLog::loadFromDb($id);
$result[$id] = $log->rerunAction();
}
return $result;
}
}
if ( !function_exists('wpmc_data_restore_action')) {
function wpmc_data_restore_action(\WPMC\Action\ActionRunner $runner) {
$trackIds = $runner->getContextIds();
$result = [];
foreach ( $trackIds as $id ) {
$track = EntityDataTrack::loadFromDb($id);
$result[$id] = $track->restore();
}
return $result;
}
}
if ( !function_exists('psCheckDbError')) {
function psCheckDbError($result) {
global $wpdb;
if ( is_wp_error( $result ) ) {
throw new \Exception( 'DB Error:' . $result->get_error_message() );
}
elseif ( empty( $result ) && !empty($wpdb->last_error) ) {
throw new \Exception( $wpdb->last_error );
}
}
}
if (!function_exists('psNiceNumber')) {
function psNiceNumber($n) {
$n = (0+str_replace(",","",$n));
if(!is_numeric($n)) return false;
if($n>1000000000000) return round(($n/1000000000000),1).'-T';
else if($n>1000000000) return round(($n/1000000000),1).'B';
else if($n>1000000) return round(($n/1000000),1).'M';
else if($n>1000) return round(($n/1000),1).'K';
return number_format($n);
}
}