-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp-sql-query.php
More file actions
137 lines (115 loc) · 4.58 KB
/
wp-sql-query.php
File metadata and controls
137 lines (115 loc) · 4.58 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
<?php
/*
Plugin Name: wp-sql-query
Plugin URI:
Version: 1.0.0
Author: Hexly
Author URI: http://www.hexly.cloud/
*/
define('HEXLY_SQL_QUERY_TYPE', 'hexly_sql_query_post');
add_action( 'plugins_loaded', [ 'HexlySqlQuery', 'get_instance' ] );
register_activation_hook( __FILE__, ['HexlySqlQuery', 'on_activate_hook'] );
register_deactivation_hook( __FILE__, ['HexlySqlQuery', 'on_deactivate_hook'] );
register_uninstall_hook( __FILE__, ['HexlySqlQuery', 'on_uninstall_hook'] );
class HexlySqlQuery {
private static $instance;
public static function get_instance() {
if ( null == self::$instance ) {
self::$instance = new HexlySqlQuery();
}
return self::$instance;
}
public static $statuses = [
['id' => 'hexsqlquery-sent', 'name' => 'Active'],
['id' => 'hexsqlquery-unsent', 'name' => 'Inactive'],
['id' => 'hexsqlquery-needs-attention', 'name' => 'Needs Attention'],
];
public function __construct() {
add_action( 'init', ['HexlySqlQuery', 'create_sql_query_post_type'] );
add_action( 'add_meta_boxes', [$this, 'custom_editor_metabox'] );
add_action( 'save_post', [$this, 'save_wp_editor_fields'] );
add_action( 'admin_post_hexly_run_sql_query', [ $this, 'run_sql_query' ]); // NOT WORKING. FIND REPLACEMENT
}
public function on_activate_hook() {
error_log('Activated!');
}
public function on_deactivate_hook() {
error_log('Dectivated!');
}
public function on_uninstall_hook() {
error_log('Uninstalled!');
}
function create_sql_query_post_type() {
$labels = [
'name' => __( 'Hexly SQL Queries' ),
'singular_name' => 'Hexly SQL Query' ,
'menu_name' => 'Hexly SQL Queries',
'all_items' => __( 'All SQL Queries', 'hexly' ),
'view_item' => __( 'View SQL Query', 'hexly' )
];
$args = [
'labels' => $labels,
'has_archive' => true,
'rewrite' => array('slug' => 'sync queries'),
'label' => __( 'Hexly SQL Queries', 'hexly' ),
'description' => 'Hexly SQL Queries',
'supports' => array( 'title' ),
'taxonomies' => array( 'genres' ),
'hierarchical' => false, // this can get slow at even just hundreds
'public' => false,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 6,
'can_export' => false,
'has_archive' => false,
'exclude_from_search' => true,
'publicly_queryable' => false,
'capability_type' => 'page'
];
$register_res = register_post_type(HEXLY_SQL_QUERY_TYPE, $args);
foreach (HexlySqlQuery::$statuses as $status) {
register_post_status( $status['id'], array(
'label' => _x( $status['name'], HEXLY_SQL_QUERY_TYPE ),
'public' => true,
'post_type' => HEXLY_SQL_QUERY_TYPE,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'show_in_metabox_dropdown' => true,
'show_in_inline_dropdown' => true,
'dashicon' => 'dashicons-yes',
'label_count' => _n_noop(
$status['name'] . ' <span class="count">(%s)</span>',
$status['name'] . ' <span class="count">(%s)</span>'
),
));
}
}
function custom_editor_metabox() {
global $post;
$post_type = $post->post_type;
$post_content = $post->post_content;
if ($post_type != HEXLY_SQL_QUERY_TYPE) {
return;
}
add_meta_box( 'custom_editor', 'SQL Editor', [$this, 'render_custom_editor_callback'], HEXLY_SQL_QUERY_TYPE, 'normal', 'high', $post_content);
}
function render_custom_editor_callback($post, $cb_args) {
$post_vars = $_POST;
$args = $cb_args['args'];
wp_editor( $args, 'hexly_sql_editor', ['tinymce' => false] );
echo '<input id="run-sql-btn" type="submit" value="Run SQL" class="button button-primary">';
}
function save_wp_editor_fields($post_id) {
$content = $_POST['hexly_sql_editor'];
remove_action( 'save_post', [$this, 'save_wp_editor_fields'] );
wp_update_post( ['ID' => $post_id, 'post_content' => wp_kses_post($content)] );
}
function run_sql_query() {
error_log(print_r('run_sql_query()', true));
$content = $_POST['hexly_sql_editor'];
error_log(print_r(['$content' => $content], true));
}
}