| @@ -1,395 +1,427 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -namespace WPDataAccess\API { | |
| 3 | +namespace WPDataAccess\API; | |
| 4 | 4 | |
| 5 | - use WPDataAccess\Query_Builder\WPDA_Query_Builder; | |
| 6 | - use WPDataAccess\WPDA; | |
| 5 | +use WPDataAccess\Data_Dictionary\WPDA_Dictionary_Lists; | |
| 6 | +use WPDataAccess\Query_Builder\WPDA_Query_Builder; | |
| 7 | +use WPDataAccess\Query_Builder\WPDA_Query_Builder_Scheduler; | |
| 8 | +use WPDataAccess\Utilities\WPDA_Mail; | |
| 9 | +use WPDataAccess\WPDA; | |
| 10 | +class WPDA_QB extends WPDA_API_Core { | |
| 11 | + const QUERY_BUILDER_AUTO_COMPLETE = 'wpda_query_builder_auto_complete'; | |
| 7 | 12 | |
| 8 | - class WPDA_QB extends WPDA_API_Core { | |
| 13 | + public function register_rest_routes() { | |
| 14 | + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/open', array( | |
| 15 | + 'methods' => array('POST'), | |
| 16 | + 'callback' => array($this, 'open'), | |
| 17 | + 'permission_callback' => '__return_true', | |
| 18 | + 'args' => array( | |
| 19 | + 'access' => $this->get_param( 'access' ), | |
| 20 | + ), | |
| 21 | + ) ); | |
| 22 | + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/run', array( | |
| 23 | + 'methods' => array('POST'), | |
| 24 | + 'callback' => array($this, 'run'), | |
| 25 | + 'permission_callback' => '__return_true', | |
| 26 | + 'args' => array( | |
| 27 | + 'dbs' => $this->get_param( 'dbs' ), | |
| 28 | + 'query' => $this->get_param( 'query' ), | |
| 29 | + 'limit' => array( | |
| 30 | + 'required' => false, | |
| 31 | + 'type' => 'string', | |
| 32 | + 'description' => __( 'Limit query output', 'wp-data-access' ), | |
| 33 | + 'sanitize_callback' => 'sanitize_text_field', | |
| 34 | + 'validate_callback' => 'rest_validate_request_arg', | |
| 35 | + ), | |
| 36 | + 'protect' => array( | |
| 37 | + 'required' => false, | |
| 38 | + 'type' => 'boolean', | |
| 39 | + 'description' => __( 'Protect WordPress tables', 'wp-data-access' ), | |
| 40 | + 'sanitize_callback' => 'sanitize_text_field', | |
| 41 | + 'validate_callback' => 'rest_validate_request_arg', | |
| 42 | + ), | |
| 43 | + 'params' => $this->get_param( 'params' ), | |
| 44 | + ), | |
| 45 | + ) ); | |
| 46 | + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/save', array( | |
| 47 | + 'methods' => array('POST'), | |
| 48 | + 'callback' => array($this, 'save'), | |
| 49 | + 'permission_callback' => '__return_true', | |
| 50 | + 'args' => array( | |
| 51 | + 'access' => $this->get_param( 'access' ), | |
| 52 | + 'dbs' => $this->get_param( 'dbs' ), | |
| 53 | + 'name' => $this->get_param( 'name' ), | |
| 54 | + 'query' => $this->get_param( 'query' ), | |
| 55 | + 'is_visual' => array( | |
| 56 | + 'required' => false, | |
| 57 | + 'type' => 'boolean', | |
| 58 | + 'description' => __( 'Is Visual Query?', 'wp-data-access' ), | |
| 59 | + 'sanitize_callback' => 'sanitize_text_field', | |
| 60 | + 'validate_callback' => 'rest_validate_request_arg', | |
| 61 | + ), | |
| 62 | + 'vqb' => $this->get_param( 'vqb' ), | |
| 63 | + 'old_name' => array( | |
| 64 | + 'required' => false, | |
| 65 | + 'type' => 'string', | |
| 66 | + 'description' => __( 'Old query name', 'wp-data-access' ), | |
| 67 | + 'sanitize_callback' => 'sanitize_text_field', | |
| 68 | + 'validate_callback' => 'rest_validate_request_arg', | |
| 69 | + ), | |
| 70 | + 'insert' => array( | |
| 71 | + 'required' => false, | |
| 72 | + 'type' => 'boolean', | |
| 73 | + 'description' => __( 'Perform insert', 'wp-data-access' ), | |
| 74 | + 'sanitize_callback' => 'sanitize_text_field', | |
| 75 | + 'validate_callback' => 'rest_validate_request_arg', | |
| 76 | + ), | |
| 77 | + 'params' => $this->get_param( 'params' ), | |
| 78 | + ), | |
| 79 | + ) ); | |
| 80 | + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/delete', array( | |
| 81 | + 'methods' => array('POST'), | |
| 82 | + 'callback' => array($this, 'delete'), | |
| 83 | + 'permission_callback' => '__return_true', | |
| 84 | + 'args' => array( | |
| 85 | + 'access' => $this->get_param( 'access' ), | |
| 86 | + 'name' => $this->get_param( 'name' ), | |
| 87 | + ), | |
| 88 | + ) ); | |
| 89 | + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/copy', array( | |
| 90 | + 'methods' => array('POST'), | |
| 91 | + 'callback' => array($this, 'copy'), | |
| 92 | + 'permission_callback' => '__return_true', | |
| 93 | + 'args' => array( | |
| 94 | + 'access_from' => $this->get_param( 'access' ), | |
| 95 | + 'from' => $this->get_param( 'name' ), | |
| 96 | + 'access_to' => $this->get_param( 'access' ), | |
| 97 | + 'to' => $this->get_param( 'name' ), | |
| 98 | + ), | |
| 99 | + ) ); | |
| 100 | + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/hints', array( | |
| 101 | + 'methods' => array('POST'), | |
| 102 | + 'callback' => array($this, 'hints'), | |
| 103 | + 'permission_callback' => '__return_true', | |
| 104 | + 'args' => array( | |
| 105 | + 'dbs' => $this->get_param( 'dbs' ), | |
| 106 | + ), | |
| 107 | + ) ); | |
| 108 | + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/ac', array( | |
| 109 | + 'methods' => array('POST'), | |
| 110 | + 'callback' => array($this, 'ac'), | |
| 111 | + 'permission_callback' => '__return_true', | |
| 112 | + 'args' => array( | |
| 113 | + 'enable' => array( | |
| 114 | + 'required' => false, | |
| 115 | + 'type' => 'boolean', | |
| 116 | + 'description' => __( 'Get|set auto complete', 'wp-data-access' ), | |
| 117 | + 'sanitize_callback' => 'sanitize_text_field', | |
| 118 | + 'validate_callback' => 'rest_validate_request_arg', | |
| 119 | + ), | |
| 120 | + ), | |
| 121 | + ) ); | |
| 122 | + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/cron/schedules', array( | |
| 123 | + 'methods' => array('POST'), | |
| 124 | + 'callback' => array($this, 'cron_schedules'), | |
| 125 | + 'permission_callback' => '__return_true', | |
| 126 | + 'args' => array(), | |
| 127 | + ) ); | |
| 128 | + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/cron/add', array( | |
| 129 | + 'methods' => array('POST'), | |
| 130 | + 'callback' => array($this, 'cron_add'), | |
| 131 | + 'permission_callback' => '__return_true', | |
| 132 | + 'args' => array( | |
| 133 | + 'access' => $this->get_param( 'access' ), | |
| 134 | + 'name' => $this->get_param( 'name' ), | |
| 135 | + 'params' => $this->get_param( 'params' ), | |
| 136 | + 'start' => array( | |
| 137 | + 'required' => true, | |
| 138 | + 'type' => 'integer', | |
| 139 | + 'description' => __( 'Start date/time', 'wp-data-access' ), | |
| 140 | + 'sanitize_callback' => 'sanitize_text_field', | |
| 141 | + 'validate_callback' => 'rest_validate_request_arg', | |
| 142 | + ), | |
| 143 | + 'interval' => array( | |
| 144 | + 'required' => true, | |
| 145 | + 'type' => 'string', | |
| 146 | + 'description' => __( 'Recurrence', 'wp-data-access' ), | |
| 147 | + 'sanitize_callback' => 'sanitize_text_field', | |
| 148 | + 'validate_callback' => 'rest_validate_request_arg', | |
| 149 | + ), | |
| 150 | + ), | |
| 151 | + ) ); | |
| 152 | + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/cron/delete', array( | |
| 153 | + 'methods' => array('POST'), | |
| 154 | + 'callback' => array($this, 'cron_delete'), | |
| 155 | + 'permission_callback' => '__return_true', | |
| 156 | + 'args' => array( | |
| 157 | + 'access' => $this->get_param( 'access' ), | |
| 158 | + 'name' => $this->get_param( 'name' ), | |
| 159 | + 'params' => $this->get_param( 'params' ), | |
| 160 | + ), | |
| 161 | + ) ); | |
| 162 | + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/vqb/get', array( | |
| 163 | + 'methods' => array('POST'), | |
| 164 | + 'callback' => array($this, 'vqb_get'), | |
| 165 | + 'permission_callback' => '__return_true', | |
| 166 | + 'args' => array( | |
| 167 | + 'access' => $this->get_param( 'access' ), | |
| 168 | + 'name' => $this->get_param( 'name' ), | |
| 169 | + ), | |
| 170 | + ) ); | |
| 171 | + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/vqb/tbl', array( | |
| 172 | + 'methods' => array('POST'), | |
| 173 | + 'callback' => array($this, 'vqb_tbl'), | |
| 174 | + 'permission_callback' => '__return_true', | |
| 175 | + 'args' => array( | |
| 176 | + 'dbs' => $this->get_param( 'dbs' ), | |
| 177 | + 'tbl' => $this->get_param( 'tbl' ), | |
| 178 | + ), | |
| 179 | + ) ); | |
| 180 | + } | |
| 9 | 181 | |
| 10 | - const QUERY_BUILDER_AUTO_COMPLETE = 'wpda_query_builder_auto_complete'; | |
| 182 | + public function vqb_tbl( $request ) { | |
| 183 | + if ( !$this->current_user_can_access() ) { | |
| 184 | + return $this->unauthorized(); | |
| 185 | + } | |
| 186 | + if ( !$this->current_user_token_valid( $request ) ) { | |
| 187 | + return $this->invalid_nonce(); | |
| 188 | + } | |
| 189 | + $dbs = $request->get_param( 'dbs' ); | |
| 190 | + $tbl = $request->get_param( 'tbl' ); | |
| 191 | + $cols = WPDA_Dictionary_Lists::get_table_widget_info( $dbs, $tbl ); | |
| 192 | + return $this->WPDA_Rest_Response( '', $cols ); | |
| 193 | + } | |
| 11 | 194 | |
| 12 | - public function register_rest_routes() { | |
| 195 | + public function vqb_get( $request ) { | |
| 196 | + if ( !$this->current_user_can_access() ) { | |
| 197 | + return $this->unauthorized(); | |
| 198 | + } | |
| 199 | + if ( !$this->current_user_token_valid( $request ) ) { | |
| 200 | + return $this->invalid_nonce(); | |
| 201 | + } | |
| 202 | + $access = $request->get_param( 'access' ); | |
| 203 | + $name = $request->get_param( 'name' ); | |
| 204 | + $qb = new WPDA_Query_Builder(); | |
| 205 | + if ( 'user' === $access ) { | |
| 206 | + $query = $qb->get_visual_query( $name ); | |
| 207 | + } else { | |
| 208 | + $query = maybe_unserialize( $qb->get_visual_query_global( $name ) ); | |
| 209 | + } | |
| 210 | + return $this->WPDA_Rest_Response( '', $query ); | |
| 211 | + } | |
| 13 | 212 | |
| 14 | - register_rest_route( | |
| 15 | - WPDA_API::WPDA_NAMESPACE, | |
| 16 | - 'qb/open', | |
| 17 | - array( | |
| 18 | - 'methods' => array( 'POST' ), | |
| 19 | - 'callback' => array( $this, 'open' ), | |
| 20 | - 'permission_callback' => '__return_true', | |
| 21 | - 'args' => array( | |
| 22 | - 'access' => $this->get_param( 'access' ), | |
| 23 | - ), | |
| 24 | - ) | |
| 25 | - ); | |
| 213 | + public function open( $request ) { | |
| 214 | + if ( !$this->current_user_can_access() ) { | |
| 215 | + return $this->unauthorized(); | |
| 216 | + } | |
| 217 | + if ( !$this->current_user_token_valid( $request ) ) { | |
| 218 | + return $this->invalid_nonce(); | |
| 219 | + } | |
| 220 | + $access = $request->get_param( 'access' ); | |
| 221 | + $qb = new WPDA_Query_Builder(); | |
| 222 | + if ( 'user' === $access ) { | |
| 223 | + $queries = $qb->get_query_list(); | |
| 224 | + } else { | |
| 225 | + $queries = $qb->get_query_list_global(); | |
| 226 | + } | |
| 227 | + if ( is_array( $queries ) ) { | |
| 228 | + uksort( $queries, 'strnatcasecmp' ); | |
| 229 | + } | |
| 230 | + return $this->WPDA_Rest_Response( '', $queries ); | |
| 231 | + } | |
| 26 | 232 | |
| 27 | - register_rest_route( | |
| 28 | - WPDA_API::WPDA_NAMESPACE, | |
| 29 | - 'qb/run', | |
| 30 | - array( | |
| 31 | - 'methods' => array( 'POST' ), | |
| 32 | - 'callback' => array( $this, 'run' ), | |
| 33 | - 'permission_callback' => '__return_true', | |
| 34 | - 'args' => array( | |
| 35 | - 'dbs' => $this->get_param( 'dbs' ), | |
| 36 | - 'query' => $this->get_param( 'query' ), | |
| 37 | - 'limit' => array( | |
| 38 | - 'required' => false, | |
| 39 | - 'type' => 'string', | |
| 40 | - 'description' => __( 'Limit query output', 'wp-data-access' ), | |
| 41 | - 'sanitize_callback' => 'sanitize_text_field', | |
| 42 | - 'validate_callback' => 'rest_validate_request_arg', | |
| 43 | - ), | |
| 44 | - 'protect' => array( | |
| 45 | - 'required' => false, | |
| 46 | - 'type' => 'boolean', | |
| 47 | - 'description' => __( 'Protect WordPress tables', 'wp-data-access' ), | |
| 48 | - 'sanitize_callback' => 'sanitize_text_field', | |
| 49 | - 'validate_callback' => 'rest_validate_request_arg', | |
| 50 | - ), | |
| 51 | - ), | |
| 52 | - ) | |
| 53 | - ); | |
| 54 | - | |
| 55 | - register_rest_route( | |
| 56 | - WPDA_API::WPDA_NAMESPACE, | |
| 57 | - 'qb/save', | |
| 58 | - array( | |
| 59 | - 'methods' => array( 'POST' ), | |
| 60 | - 'callback' => array( $this, 'save' ), | |
| 61 | - 'permission_callback' => '__return_true', | |
| 62 | - 'args' => array( | |
| 63 | - 'access' => $this->get_param( 'access' ), | |
| 64 | - 'dbs' => $this->get_param( 'dbs' ), | |
| 65 | - 'name' => $this->get_param( 'name' ), | |
| 66 | - 'query' => $this->get_param( 'query' ), | |
| 67 | - 'vqb' => $this->get_param( 'vqb' ), | |
| 68 | - 'old_name' => array( | |
| 69 | - 'required' => false, | |
| 70 | - 'type' => 'string', | |
| 71 | - 'description' => __( 'Old query name', 'wp-data-access' ), | |
| 72 | - 'sanitize_callback' => 'sanitize_text_field', | |
| 73 | - 'validate_callback' => 'rest_validate_request_arg', | |
| 74 | - ), | |
| 75 | - 'insert' => array( | |
| 76 | - 'required' => false, | |
| 77 | - 'type' => 'boolean', | |
| 78 | - 'description' => __( 'Perform insert', 'wp-data-access' ), | |
| 79 | - 'sanitize_callback' => 'sanitize_text_field', | |
| 80 | - 'validate_callback' => 'rest_validate_request_arg', | |
| 81 | - ), | |
| 82 | - ), | |
| 83 | - ) | |
| 84 | - ); | |
| 85 | - | |
| 86 | - register_rest_route( | |
| 87 | - WPDA_API::WPDA_NAMESPACE, | |
| 88 | - 'qb/delete', | |
| 89 | - array( | |
| 90 | - 'methods' => array( 'POST' ), | |
| 91 | - 'callback' => array( $this, 'delete' ), | |
| 92 | - 'permission_callback' => '__return_true', | |
| 93 | - 'args' => array( | |
| 94 | - 'access' => $this->get_param( 'access' ), | |
| 95 | - 'name' => $this->get_param( 'name' ), | |
| 96 | - ), | |
| 97 | - ) | |
| 98 | - ); | |
| 99 | - | |
| 100 | - register_rest_route( | |
| 101 | - WPDA_API::WPDA_NAMESPACE, | |
| 102 | - 'qb/copy', | |
| 103 | - array( | |
| 104 | - 'methods' => array( 'POST' ), | |
| 105 | - 'callback' => array( $this, 'copy' ), | |
| 106 | - 'permission_callback' => '__return_true', | |
| 107 | - 'args' => array( | |
| 108 | - 'access_from' => $this->get_param( 'access' ), | |
| 109 | - 'from' => $this->get_param( 'name' ), | |
| 110 | - 'access_to' => $this->get_param( 'access' ), | |
| 111 | - 'to' => $this->get_param( 'name' ), | |
| 112 | - ), | |
| 113 | - ) | |
| 114 | - ); | |
| 115 | - | |
| 116 | - register_rest_route( | |
| 117 | - WPDA_API::WPDA_NAMESPACE, | |
| 118 | - 'qb/hints', | |
| 119 | - array( | |
| 120 | - 'methods' => array( 'POST' ), | |
| 121 | - 'callback' => array( $this, 'hints' ), | |
| 122 | - 'permission_callback' => '__return_true', | |
| 123 | - 'args' => array( | |
| 124 | - 'dbs' => $this->get_param( 'dbs' ), | |
| 125 | - ), | |
| 126 | - ) | |
| 127 | - ); | |
| 128 | - | |
| 129 | - register_rest_route( | |
| 130 | - WPDA_API::WPDA_NAMESPACE, | |
| 131 | - 'qb/ac', | |
| 132 | - array( | |
| 133 | - 'methods' => array( 'POST' ), | |
| 134 | - 'callback' => array( $this, 'ac' ), | |
| 135 | - 'permission_callback' => '__return_true', | |
| 136 | - 'args' => array( | |
| 137 | - 'enable' => array( | |
| 138 | - 'required' => false, | |
| 139 | - 'type' => 'boolean', | |
| 140 | - 'description' => __( 'Get|set auto complete', 'wp-data-access' ), | |
| 141 | - 'sanitize_callback' => 'sanitize_text_field', | |
| 142 | - 'validate_callback' => 'rest_validate_request_arg', | |
| 143 | - ), | |
| 144 | - ), | |
| 145 | - ) | |
| 146 | - ); | |
| 147 | - | |
| 233 | + public function run( $request ) { | |
| 234 | + if ( !$this->current_user_can_access() ) { | |
| 235 | + return $this->unauthorized(); | |
| 148 | 236 | } |
| 237 | + if ( !$this->current_user_token_valid( $request ) ) { | |
| 238 | + return $this->invalid_nonce(); | |
| 239 | + } | |
| 240 | + $dbs = $request->get_param( 'dbs' ); | |
| 241 | + $query = $request->get_param( 'query' ); | |
| 242 | + $limit = $request->get_param( 'limit' ); | |
| 243 | + $protect = $request->get_param( 'protect' ); | |
| 244 | + $params = $request->get_param( 'params' ); | |
| 245 | + $qb = new WPDA_Query_Builder(); | |
| 246 | + return $this->WPDA_Rest_Response( '', $qb->execute_query( | |
| 247 | + $dbs, | |
| 248 | + $query, | |
| 249 | + $limit, | |
| 250 | + $protect, | |
| 251 | + $params | |
| 252 | + ) ); | |
| 253 | + } | |
| 149 | 254 | |
| 150 | - public function open( $request ) { | |
| 151 | - | |
| 152 | - if ( ! $this->current_user_can_access() ) { | |
| 153 | - return $this->unauthorized(); | |
| 154 | - } | |
| 155 | - | |
| 156 | - if ( ! $this->current_user_token_valid( $request ) ) { | |
| 157 | - return $this->invalid_nonce(); | |
| 158 | - } | |
| 159 | - | |
| 160 | - $access = $request->get_param( 'access' ); | |
| 161 | - | |
| 162 | - $qb = new WPDA_Query_Builder(); | |
| 163 | - if ( 'user' === $access ) { | |
| 164 | - $queries = $qb->get_query_list(); | |
| 165 | - } else { | |
| 166 | - $queries = $qb->get_query_list_global(); | |
| 167 | - } | |
| 168 | - if ( is_array( $queries ) ) { | |
| 169 | - uksort( $queries, 'strnatcasecmp' ); | |
| 170 | - } | |
| 171 | - | |
| 172 | - return $this->WPDA_Rest_Response( | |
| 173 | - '', | |
| 174 | - $queries, | |
| 175 | - ); | |
| 176 | - | |
| 255 | + public function save( $request ) { | |
| 256 | + if ( !$this->current_user_can_access() ) { | |
| 257 | + return $this->unauthorized(); | |
| 177 | 258 | } |
| 178 | - | |
| 179 | - public function run( $request ) { | |
| 180 | - | |
| 181 | - if ( ! $this->current_user_can_access() ) { | |
| 182 | - return $this->unauthorized(); | |
| 183 | - } | |
| 184 | - | |
| 185 | - if ( ! $this->current_user_token_valid( $request ) ) { | |
| 186 | - return $this->invalid_nonce(); | |
| 187 | - } | |
| 188 | - | |
| 189 | - $dbs = $request->get_param( 'dbs' ); | |
| 190 | - $query = $request->get_param( 'query' ); | |
| 191 | - $limit = $request->get_param( 'limit' ); | |
| 192 | - $protect = $request->get_param( 'protect' ); | |
| 193 | - | |
| 194 | - $qb = new WPDA_Query_Builder(); | |
| 195 | - | |
| 196 | - return $this->WPDA_Rest_Response( | |
| 197 | - '', | |
| 198 | - $qb->execute_query( $dbs, $query, $limit, $protect ) | |
| 199 | - ); | |
| 200 | - | |
| 259 | + if ( !$this->current_user_token_valid( $request ) ) { | |
| 260 | + return $this->invalid_nonce(); | |
| 201 | 261 | } |
| 202 | - | |
| 203 | - public function save( $request ) { | |
| 204 | - | |
| 205 | - if ( ! $this->current_user_can_access() ) { | |
| 206 | - return $this->unauthorized(); | |
| 262 | + $access = $request->get_param( 'access' ); | |
| 263 | + $dbs = $request->get_param( 'dbs' ); | |
| 264 | + $name = $request->get_param( 'name' ); | |
| 265 | + $query = $request->get_param( 'query' ); | |
| 266 | + $is_visual = '1' === $request->get_param( 'is_visual' ); | |
| 267 | + $vqb = $request->get_param( 'vqb' ); | |
| 268 | + $old_name = $request->get_param( 'old_name' ) ?? ''; | |
| 269 | + $insert = '1' === $request->get_param( 'insert' ); | |
| 270 | + $params = $request->get_param( 'params' ); | |
| 271 | + $qb = new WPDA_Query_Builder(); | |
| 272 | + $saved = array(); | |
| 273 | + if ( 'user' === $access ) { | |
| 274 | + if ( $insert ) { | |
| 275 | + // Check if query name is already used | |
| 276 | + $saved = $qb->get_query( $name ); | |
| 207 | 277 | } |
| 208 | - | |
| 209 | - if ( ! $this->current_user_token_valid( $request ) ) { | |
| 210 | - return $this->invalid_nonce(); | |
| 278 | + if ( 0 === count( $saved ) ) { | |
| 279 | + $qb->update_query( | |
| 280 | + $dbs, | |
| 281 | + $name, | |
| 282 | + $query, | |
| 283 | + $old_name, | |
| 284 | + $is_visual, | |
| 285 | + $vqb, | |
| 286 | + $params | |
| 287 | + ); | |
| 211 | 288 | } |
| 212 | - | |
| 213 | - $access = $request->get_param( 'access' ); | |
| 214 | - $dbs = $request->get_param( 'dbs' ); | |
| 215 | - $name = $request->get_param( 'name' ); | |
| 216 | - $query = $request->get_param( 'query' ); | |
| 217 | - $vqb = $request->get_param( 'vqb' ); | |
| 218 | - $old_name = $request->get_param( 'old_name' ) ?? ''; | |
| 219 | - $insert = '1' === $request->get_param( 'insert' ); | |
| 220 | - | |
| 221 | - $qb = new WPDA_Query_Builder(); | |
| 222 | - $saved = array(); | |
| 223 | - if ( 'user' === $access ) { | |
| 224 | - if ( $insert ) { | |
| 225 | - // Check if query name is already used | |
| 226 | - $saved = $qb->get_query( $name ); | |
| 227 | - } | |
| 228 | - | |
| 229 | - if ( 0 === count( $saved )) { | |
| 230 | - $qb->update_query( $dbs, $name, $query, $old_name, $vqb ); | |
| 231 | - } | |
| 232 | - } else { | |
| 233 | - if ( $insert ) { | |
| 234 | - // Check if query name is already used | |
| 235 | - $saved = $qb->get_query_global( $name ); | |
| 236 | - } | |
| 237 | - | |
| 238 | - if ( 0 === count( $saved )) { | |
| 239 | - $qb->update_query_global($dbs, $name, $query, $old_name, $vqb); | |
| 240 | - } | |
| 289 | + } else { | |
| 290 | + if ( $insert ) { | |
| 291 | + // Check if query name is already used | |
| 292 | + $saved = $qb->get_query_global( $name ); | |
| 241 | 293 | } |
| 242 | - | |
| 243 | - if ( 0 === count( $saved )) { | |
| 244 | - return $this->WPDA_Rest_Response( | |
| 245 | - '' | |
| 294 | + if ( 0 === count( $saved ) ) { | |
| 295 | + $qb->update_query_global( | |
| 296 | + $dbs, | |
| 297 | + $name, | |
| 298 | + $query, | |
| 299 | + $old_name, | |
| 300 | + $is_visual, | |
| 301 | + $vqb, | |
| 302 | + $params | |
| 246 | 303 | ); |
| 247 | - } else { | |
| 248 | - return new \WP_Error( | |
| 249 | - 'error', | |
| 250 | - 'Query name already exists', | |
| 251 | - array( 'status' => 403 ) | |
| 252 | - ); | |
| 253 | 304 | } |
| 305 | + } | |
| 306 | + if ( 0 === count( $saved ) ) { | |
| 307 | + return $this->WPDA_Rest_Response( '' ); | |
| 308 | + } else { | |
| 309 | + return new \WP_Error('error', 'Query name already exists', array( | |
| 310 | + 'status' => 403, | |
| 311 | + )); | |
| 312 | + } | |
| 313 | + } | |
| 254 | 314 | |
| 315 | + public function delete( $request ) { | |
| 316 | + if ( !$this->current_user_can_access() ) { | |
| 317 | + return $this->unauthorized(); | |
| 255 | 318 | } |
| 319 | + if ( !$this->current_user_token_valid( $request ) ) { | |
| 320 | + return $this->invalid_nonce(); | |
| 321 | + } | |
| 322 | + $access = $request->get_param( 'access' ); | |
| 323 | + $name = $request->get_param( 'name' ); | |
| 324 | + $qb = new WPDA_Query_Builder(); | |
| 325 | + if ( 'user' === $access ) { | |
| 326 | + $qb->delete_query( $name ); | |
| 327 | + } else { | |
| 328 | + $qb->delete_query_global( $name ); | |
| 329 | + } | |
| 330 | + return $this->WPDA_Rest_Response( '' ); | |
| 331 | + } | |
| 256 | 332 | |
| 257 | - public function delete( $request ) { | |
| 258 | - | |
| 259 | - if ( ! $this->current_user_can_access() ) { | |
| 260 | - return $this->unauthorized(); | |
| 261 | - } | |
| 262 | - | |
| 263 | - if ( ! $this->current_user_token_valid( $request ) ) { | |
| 264 | - return $this->invalid_nonce(); | |
| 265 | - } | |
| 266 | - | |
| 267 | - $access = $request->get_param( 'access' ); | |
| 268 | - $name = $request->get_param( 'name' ); | |
| 269 | - | |
| 270 | - $qb = new WPDA_Query_Builder(); | |
| 271 | - if ( 'user' === $access ) { | |
| 272 | - $qb->delete_query($name); | |
| 273 | - } else { | |
| 274 | - $qb->delete_query_global($name); | |
| 275 | - } | |
| 276 | - | |
| 277 | - return $this->WPDA_Rest_Response( | |
| 278 | - '' | |
| 279 | - ); | |
| 280 | - | |
| 333 | + public function copy( $request ) { | |
| 334 | + if ( !$this->current_user_can_access() ) { | |
| 335 | + return $this->unauthorized(); | |
| 281 | 336 | } |
| 282 | - | |
| 283 | - public function copy( $request ) { | |
| 284 | - | |
| 285 | - if ( ! $this->current_user_can_access() ) { | |
| 286 | - return $this->unauthorized(); | |
| 337 | + if ( !$this->current_user_token_valid( $request ) ) { | |
| 338 | + return $this->invalid_nonce(); | |
| 339 | + } | |
| 340 | + $access_from = $request->get_param( 'access_from' ); | |
| 341 | + $from_name = $request->get_param( 'from' ); | |
| 342 | + $access_to = $request->get_param( 'access_to' ); | |
| 343 | + $to_name = $request->get_param( 'to' ); | |
| 344 | + $qb = new WPDA_Query_Builder(); | |
| 345 | + $source = ( 'user' === $access_from ? $qb->get_query( $from_name ) : $qb->get_query_global( $from_name ) ); | |
| 346 | + $visual = ( 'user' === $access_from ? $qb->get_visual_query( $from_name ) : maybe_unserialize( $qb->get_visual_query_global( $from_name ) ) ); | |
| 347 | + if ( 0 === count( $source ) ) { | |
| 348 | + return new \WP_Error('error', 'Source query not found', array( | |
| 349 | + 'status' => 403, | |
| 350 | + )); | |
| 351 | + } | |
| 352 | + if ( 'user' === $access_to ) { | |
| 353 | + $saved = $qb->get_query( $to_name ); | |
| 354 | + if ( 0 === count( $saved ) ) { | |
| 355 | + $qb->add_query( $to_name, $source ); | |
| 356 | + if ( !empty( $visual ) ) { | |
| 357 | + $qb->upd_visual_query( $to_name, $visual ); | |
| 358 | + } | |
| 287 | 359 | } |
| 288 | - | |
| 289 | - if ( ! $this->current_user_token_valid( $request ) ) { | |
| 290 | - return $this->invalid_nonce(); | |
| 291 | - } | |
| 292 | - | |
| 293 | - $access_from = $request->get_param( 'access_from' ); | |
| 294 | - $from_name = $request->get_param( 'from' ); | |
| 295 | - $access_to = $request->get_param( 'access_to' ); | |
| 296 | - $to_name = $request->get_param( 'to' ); | |
| 297 | - | |
| 298 | - $qb = new WPDA_Query_Builder(); | |
| 299 | - $source = 'user' === $access_from ? $qb->get_query( $from_name ) : $qb->get_query_global( $from_name ); | |
| 300 | - $saved = array(); | |
| 301 | - if ( 'user' === $access_to ) { | |
| 302 | - $saved = $qb->get_query( $to_name ); | |
| 303 | - if ( 0 === count( $saved )) { | |
| 304 | - $qb->add_query($to_name, $source); | |
| 360 | + } else { | |
| 361 | + $saved = $qb->get_query_global( $to_name ); | |
| 362 | + if ( 0 === count( $saved ) ) { | |
| 363 | + $qb->add_query_global( $to_name, $source ); | |
| 364 | + if ( !empty( $visual ) ) { | |
| 365 | + $qb->upd_visual_query_global( $to_name, $visual ); | |
| 305 | 366 | } |
| 306 | - } else { | |
| 307 | - $saved = $qb->get_query_global( $to_name ); | |
| 308 | - if ( 0 === count( $saved )) { | |
| 309 | - $qb->add_query_global($to_name, $source); | |
| 310 | - } | |
| 311 | 367 | } |
| 368 | + } | |
| 369 | + if ( 0 === count( $saved ) ) { | |
| 370 | + return $this->WPDA_Rest_Response( '' ); | |
| 371 | + } else { | |
| 372 | + return new \WP_Error('error', 'Query name already exists', array( | |
| 373 | + 'status' => 403, | |
| 374 | + )); | |
| 375 | + } | |
| 376 | + } | |
| 312 | 377 | |
| 313 | - if ( 0 === count( $saved )) { | |
| 314 | - return $this->WPDA_Rest_Response( | |
| 315 | - '' | |
| 316 | - ); | |
| 317 | - } else { | |
| 318 | - return new \WP_Error( | |
| 319 | - 'error', | |
| 320 | - 'Query name already exists', | |
| 321 | - array( 'status' => 403 ) | |
| 322 | - ); | |
| 323 | - } | |
| 378 | + public function hints( $request ) { | |
| 379 | + if ( !$this->current_user_can_access() ) { | |
| 380 | + return $this->unauthorized(); | |
| 381 | + } | |
| 382 | + if ( !$this->current_user_token_valid( $request ) ) { | |
| 383 | + return $this->invalid_nonce(); | |
| 384 | + } | |
| 385 | + $dbs = $request->get_param( 'dbs' ); | |
| 386 | + $qb = new WPDA_Query_Builder(); | |
| 387 | + return $this->WPDA_Rest_Response( '', $qb->get_hints( $dbs ) ); | |
| 388 | + } | |
| 324 | 389 | |
| 390 | + public function ac( $request ) { | |
| 391 | + if ( !$this->current_user_can_access() ) { | |
| 392 | + return $this->unauthorized(); | |
| 325 | 393 | } |
| 326 | - | |
| 327 | - public function hints( $request ) { | |
| 328 | - | |
| 329 | - if ( ! $this->current_user_can_access() ) { | |
| 330 | - return $this->unauthorized(); | |
| 331 | - } | |
| 332 | - | |
| 333 | - if ( ! $this->current_user_token_valid( $request ) ) { | |
| 334 | - return $this->invalid_nonce(); | |
| 335 | - } | |
| 336 | - | |
| 337 | - $dbs = $request->get_param( 'dbs' ); | |
| 338 | - | |
| 339 | - $qb = new WPDA_Query_Builder(); | |
| 340 | - return $this->WPDA_Rest_Response( | |
| 341 | - '', | |
| 342 | - $qb->get_hints( $dbs ), | |
| 343 | - ); | |
| 344 | - | |
| 394 | + if ( !$this->current_user_token_valid( $request ) ) { | |
| 395 | + return $this->invalid_nonce(); | |
| 345 | 396 | } |
| 397 | + $enable = $request->get_param( 'enable' ); | |
| 398 | + if ( null === $enable ) { | |
| 399 | + // Return current value | |
| 400 | + return $this->WPDA_Rest_Response( get_user_meta( WPDA::get_current_user_id(), self::QUERY_BUILDER_AUTO_COMPLETE, true ) ); | |
| 401 | + } elseif ( '1' == $enable ) { | |
| 402 | + // Enable auto complete | |
| 403 | + update_user_meta( WPDA::get_current_user_id(), self::QUERY_BUILDER_AUTO_COMPLETE, true ); | |
| 404 | + return $this->WPDA_Rest_Response( '' ); | |
| 405 | + } else { | |
| 406 | + // Enable auto complete | |
| 407 | + update_user_meta( WPDA::get_current_user_id(), self::QUERY_BUILDER_AUTO_COMPLETE, false ); | |
| 408 | + return $this->WPDA_Rest_Response( '' ); | |
| 409 | + } | |
| 410 | + } | |
| 346 | 411 | |
| 347 | - public function ac( $request ) { | |
| 412 | + public function cron_schedules( $request ) { | |
| 413 | + // This feature is implemented in the premium version | |
| 414 | + return $this->unauthorized(); | |
| 415 | + } | |
| 348 | 416 | |
| 349 | - if ( ! $this->current_user_can_access() ) { | |
| 350 | - return $this->unauthorized(); | |
| 351 | - } | |
| 417 | + public function cron_add( $request ) { | |
| 418 | + // This feature is implemented in the premium version | |
| 419 | + return $this->unauthorized(); | |
| 420 | + } | |
| 352 | 421 | |
| 353 | - if ( ! $this->current_user_token_valid( $request ) ) { | |
| 354 | - return $this->invalid_nonce(); | |
| 355 | - } | |
| 356 | - | |
| 357 | - $enable = $request->get_param( 'enable' ); | |
| 358 | - if ( null === $enable ) { | |
| 359 | - // Return current value | |
| 360 | - return $this->WPDA_Rest_Response( | |
| 361 | - get_user_meta( | |
| 362 | - WPDA::get_current_user_id(), | |
| 363 | - self::QUERY_BUILDER_AUTO_COMPLETE, | |
| 364 | - true | |
| 365 | - ) | |
| 366 | - ); | |
| 367 | - } elseif ( '1' == $enable ) { | |
| 368 | - // Enable auto complete | |
| 369 | - update_user_meta( | |
| 370 | - WPDA::get_current_user_id(), | |
| 371 | - self::QUERY_BUILDER_AUTO_COMPLETE, | |
| 372 | - true | |
| 373 | - ); | |
| 374 | - | |
| 375 | - return $this->WPDA_Rest_Response( | |
| 376 | - '' | |
| 377 | - ); | |
| 378 | - } else { | |
| 379 | - // Enable auto complete | |
| 380 | - update_user_meta( | |
| 381 | - WPDA::get_current_user_id(), | |
| 382 | - self::QUERY_BUILDER_AUTO_COMPLETE, | |
| 383 | - false | |
| 384 | - ); | |
| 385 | - | |
| 386 | - return $this->WPDA_Rest_Response( | |
| 387 | - '' | |
| 388 | - ); | |
| 389 | - } | |
| 390 | - | |
| 391 | - } | |
| 392 | - | |
| 422 | + public function cron_delete( $request ) { | |
| 423 | + // This feature is implemented in the premium version | |
| 424 | + return $this->unauthorized(); | |
| 393 | 425 | } |
| 394 | 426 | |
| 395 | -} | |
| 427 | +} | |