| @@ -1,0 +1,305 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Custom Tables | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Custom_Tables | |
| 6 | + * @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.0.0 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +// Custom Tables | |
| 13 | +require_once AUTOMATORWP_DIR . 'includes/custom-tables/automations.php'; | |
| 14 | +require_once AUTOMATORWP_DIR . 'includes/custom-tables/triggers.php'; | |
| 15 | +require_once AUTOMATORWP_DIR . 'includes/custom-tables/actions.php'; | |
| 16 | +require_once AUTOMATORWP_DIR . 'includes/custom-tables/logs.php'; | |
| 17 | + | |
| 18 | +/** | |
| 19 | + * Register all custom database Tables | |
| 20 | + * | |
| 21 | + * @since 1.0.0 | |
| 22 | + * | |
| 23 | + * @return void | |
| 24 | + */ | |
| 25 | +function automatorwp_register_custom_tables() { | |
| 26 | + | |
| 27 | + // Automations | |
| 28 | + ct_register_table( 'automatorwp_automations', array( | |
| 29 | + 'show_ui' => true, | |
| 30 | + 'show_in_rest' => true, | |
| 31 | + 'rest_base' => 'automatorwp-automations', | |
| 32 | + 'version' => 4, | |
| 33 | + 'capability' => automatorwp_get_manager_capability(), | |
| 34 | + 'supports' => array( 'meta' ), | |
| 35 | + 'views' => array( | |
| 36 | + 'list' => array( | |
| 37 | + 'parent_slug' => 'automatorwp', | |
| 38 | + 'priority' => 10, | |
| 39 | + ), | |
| 40 | + ), | |
| 41 | + 'schema' => array( | |
| 42 | + 'id' => array( | |
| 43 | + 'type' => 'bigint', | |
| 44 | + 'length' => '20', | |
| 45 | + 'auto_increment' => true, | |
| 46 | + 'primary_key' => true, | |
| 47 | + ), | |
| 48 | + 'title' => array( | |
| 49 | + 'type' => 'text', | |
| 50 | + ), | |
| 51 | + 'type' => array( | |
| 52 | + 'type' => 'text', | |
| 53 | + ), | |
| 54 | + 'user_id' => array( | |
| 55 | + 'type' => 'bigint', | |
| 56 | + 'length' => '20', | |
| 57 | + 'key' => true, | |
| 58 | + ), | |
| 59 | + 'sequential' => array( | |
| 60 | + 'type' => 'tinyint', | |
| 61 | + 'length' => '1', | |
| 62 | + 'default' => '0', | |
| 63 | + ), | |
| 64 | + 'times_per_user' => array( | |
| 65 | + 'type' => 'int', | |
| 66 | + 'length' => '11', | |
| 67 | + 'default' => '1', | |
| 68 | + ), | |
| 69 | + 'times' => array( | |
| 70 | + 'type' => 'int', | |
| 71 | + 'length' => '11', | |
| 72 | + 'default' => '0', | |
| 73 | + ), | |
| 74 | + 'status' => array( | |
| 75 | + 'type' => 'varchar', | |
| 76 | + 'length' => '50', | |
| 77 | + ), | |
| 78 | + 'date' => array( | |
| 79 | + 'type' => 'datetime', | |
| 80 | + 'default' => '0000-00-00 00:00:00' | |
| 81 | + ), | |
| 82 | + 'expiration' => array( | |
| 83 | + 'type' => 'datetime', | |
| 84 | + 'default' => '0000-00-00 00:00:00' | |
| 85 | + ), | |
| 86 | + ), | |
| 87 | + ) ); | |
| 88 | + | |
| 89 | + // Triggers | |
| 90 | + ct_register_table( 'automatorwp_triggers', array( | |
| 91 | + 'show_ui' => false, | |
| 92 | + 'show_in_rest' => true, | |
| 93 | + 'rest_base' => 'automatorwp-triggers', | |
| 94 | + 'version' => 2, | |
| 95 | + 'capability' => automatorwp_get_manager_capability(), | |
| 96 | + 'supports' => array( 'meta' ), | |
| 97 | + 'schema' => array( | |
| 98 | + 'id' => array( | |
| 99 | + 'type' => 'bigint', | |
| 100 | + 'length' => '20', | |
| 101 | + 'auto_increment' => true, | |
| 102 | + 'primary_key' => true, | |
| 103 | + ), | |
| 104 | + 'automation_id' => array( | |
| 105 | + 'type' => 'bigint', | |
| 106 | + 'length' => '20', | |
| 107 | + 'key' => true, | |
| 108 | + ), | |
| 109 | + 'title' => array( | |
| 110 | + 'type' => 'text', | |
| 111 | + ), | |
| 112 | + 'type' => array( | |
| 113 | + 'type' => 'text', | |
| 114 | + ), | |
| 115 | + 'status' => array( | |
| 116 | + 'type' => 'varchar', | |
| 117 | + 'length' => '50', | |
| 118 | + ), | |
| 119 | + 'position' => array( | |
| 120 | + 'type' => 'int', | |
| 121 | + 'length' => '11', | |
| 122 | + 'default' => '0', | |
| 123 | + ), | |
| 124 | + 'date' => array( | |
| 125 | + 'type' => 'datetime', | |
| 126 | + 'default' => '0000-00-00 00:00:00' | |
| 127 | + ), | |
| 128 | + ), | |
| 129 | + ) ); | |
| 130 | + | |
| 131 | + // Actions | |
| 132 | + ct_register_table( 'automatorwp_actions', array( | |
| 133 | + 'show_ui' => false, | |
| 134 | + 'show_in_rest' => true, | |
| 135 | + 'rest_base' => 'automatorwp-actions', | |
| 136 | + 'version' => 2, | |
| 137 | + 'capability' => automatorwp_get_manager_capability(), | |
| 138 | + 'supports' => array( 'meta' ), | |
| 139 | + 'schema' => array( | |
| 140 | + 'id' => array( | |
| 141 | + 'type' => 'bigint', | |
| 142 | + 'length' => '20', | |
| 143 | + 'auto_increment' => true, | |
| 144 | + 'primary_key' => true, | |
| 145 | + ), | |
| 146 | + 'automation_id' => array( | |
| 147 | + 'type' => 'bigint', | |
| 148 | + 'length' => '20', | |
| 149 | + 'key' => true, | |
| 150 | + ), | |
| 151 | + 'title' => array( | |
| 152 | + 'type' => 'text', | |
| 153 | + ), | |
| 154 | + 'type' => array( | |
| 155 | + 'type' => 'text', | |
| 156 | + ), | |
| 157 | + 'status' => array( | |
| 158 | + 'type' => 'varchar', | |
| 159 | + 'length' => '50', | |
| 160 | + ), | |
| 161 | + 'position' => array( | |
| 162 | + 'type' => 'int', | |
| 163 | + 'length' => '11', | |
| 164 | + 'default' => '0', | |
| 165 | + ), | |
| 166 | + 'date' => array( | |
| 167 | + 'type' => 'datetime', | |
| 168 | + 'default' => '0000-00-00 00:00:00' | |
| 169 | + ), | |
| 170 | + ), | |
| 171 | + ) ); | |
| 172 | + | |
| 173 | + // Logs | |
| 174 | + ct_register_table( 'automatorwp_logs', array( | |
| 175 | + 'show_ui' => true, | |
| 176 | + 'show_in_rest' => true, | |
| 177 | + 'rest_base' => 'automatorwp-logs', | |
| 178 | + 'version' => 2, | |
| 179 | + 'capability' => automatorwp_get_manager_capability(), | |
| 180 | + 'supports' => array( 'meta' ), | |
| 181 | + 'views' => array( | |
| 182 | + 'list' => array( | |
| 183 | + 'parent_slug' => 'automatorwp', | |
| 184 | + 'priority' => 10, | |
| 185 | + ), | |
| 186 | + 'add' => false, | |
| 187 | + ), | |
| 188 | + 'schema' => array( | |
| 189 | + 'id' => array( | |
| 190 | + 'type' => 'bigint', | |
| 191 | + 'length' => '20', | |
| 192 | + 'auto_increment' => true, | |
| 193 | + 'primary_key' => true, | |
| 194 | + ), | |
| 195 | + 'title' => array( | |
| 196 | + 'type' => 'text', | |
| 197 | + ), | |
| 198 | + 'type' => array( | |
| 199 | + 'type' => 'text', | |
| 200 | + ), | |
| 201 | + 'object_id' => array( | |
| 202 | + 'type' => 'bigint', | |
| 203 | + 'length' => '20', | |
| 204 | + 'key' => true, | |
| 205 | + ), | |
| 206 | + 'user_id' => array( | |
| 207 | + 'type' => 'bigint', | |
| 208 | + 'length' => '20', | |
| 209 | + 'key' => true, | |
| 210 | + ), | |
| 211 | + 'post_id' => array( | |
| 212 | + 'type' => 'bigint', | |
| 213 | + 'length' => '20', | |
| 214 | + 'key' => true, | |
| 215 | + ), | |
| 216 | + 'date' => array( | |
| 217 | + 'type' => 'datetime', | |
| 218 | + 'default' => '0000-00-00 00:00:00' | |
| 219 | + ), | |
| 220 | + ), | |
| 221 | + ) ); | |
| 222 | + | |
| 223 | +} | |
| 224 | +add_action( 'ct_init', 'automatorwp_register_custom_tables' ); | |
| 225 | + | |
| 226 | +/** | |
| 227 | + * Helper function to generate a where from a CT Query | |
| 228 | + * | |
| 229 | + * @since 1.0.0 | |
| 230 | + * | |
| 231 | + * @param array $query_vars The query vars | |
| 232 | + * @param string $field_id The field id | |
| 233 | + * @param string $table_field The table field key | |
| 234 | + * @param string $field_type The field type (string|integer) | |
| 235 | + * @param string $single_operator The single operator (=|!=) | |
| 236 | + * @param string $array_operator The array operator (IN|NOT IN) | |
| 237 | + * | |
| 238 | + * @return string | |
| 239 | + */ | |
| 240 | +function automatorwp_custom_table_where( $query_vars, $field_id, $table_field = '', $field_type = 'string', $single_operator = '=', $array_operator = 'IN' ) { | |
| 241 | + | |
| 242 | + global $ct_table; | |
| 243 | + | |
| 244 | + $table_name = $ct_table->db->table_name; | |
| 245 | + | |
| 246 | + $where = ''; | |
| 247 | + | |
| 248 | + // Backward compatibility for automatorwp_custom_table_where( $query_vars, $field_id, $field_type ) | |
| 249 | + if( in_array( $table_field, array( 'string', 'integer', 'text', 'int' ) ) ) { | |
| 250 | + $table_field = $field_id; | |
| 251 | + } | |
| 252 | + | |
| 253 | + // Shorthand | |
| 254 | + $qv = $query_vars; | |
| 255 | + | |
| 256 | + // Type | |
| 257 | + if( isset( $qv[$field_id] ) && ! empty( $qv[$field_id] ) ) { | |
| 258 | + | |
| 259 | + if( is_array( $qv[$field_id] ) ) { | |
| 260 | + // Multiples values | |
| 261 | + | |
| 262 | + if( $field_type === 'string' || $field_type === 'text' ) { | |
| 263 | + | |
| 264 | + // Sanitize | |
| 265 | + $value = array_map( 'esc_sql', $qv[$field_id] ); | |
| 266 | + | |
| 267 | + // Join values by a comma-separated list of strings | |
| 268 | + $value = "'" . implode( "', '", $value ) . "'"; | |
| 269 | + | |
| 270 | + $where .= " AND {$table_name}.{$table_field} {$array_operator} ( {$value} )"; | |
| 271 | + | |
| 272 | + } else if( $field_type === 'integer' || $field_type === 'int' ) { | |
| 273 | + | |
| 274 | + // Sanitize | |
| 275 | + $value = array_map( 'absint', $qv[$field_id] ); | |
| 276 | + | |
| 277 | + // Join values by a comma-separated list of integers | |
| 278 | + $value = "'" . implode( ", ", $value ) . "'"; | |
| 279 | + | |
| 280 | + $where .= " AND {$table_name}.{$table_field} {$array_operator} ( {$value} )"; | |
| 281 | + | |
| 282 | + } | |
| 283 | + } else { | |
| 284 | + // Single value | |
| 285 | + | |
| 286 | + if( $field_type === 'string' || $field_type === 'text' ) { | |
| 287 | + | |
| 288 | + $value = esc_sql( $qv[$field_id] ); | |
| 289 | + | |
| 290 | + $where .= " AND {$table_name}.{$table_field} {$single_operator} '{$value}'"; | |
| 291 | + | |
| 292 | + } else if( $field_type === 'integer' || $field_type === 'int' ) { | |
| 293 | + | |
| 294 | + $value = absint( $qv[$field_id] ); | |
| 295 | + | |
| 296 | + $where .= " AND {$table_name}.{$table_field} {$single_operator} {$value}"; | |
| 297 | + | |
| 298 | + } | |
| 299 | + } | |
| 300 | + | |
| 301 | + } | |
| 302 | + | |
| 303 | + return $where; | |
| 304 | + | |
| 305 | +} | |