Complex classes like FrmDb often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FrmDb, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class FrmDb { |
||
| 4 | var $fields; |
||
| 5 | var $forms; |
||
| 6 | var $entries; |
||
| 7 | var $entry_metas; |
||
| 8 | |||
| 9 | public function __construct() { |
||
| 10 | if ( ! defined('ABSPATH') ) { |
||
| 11 | die('You are not allowed to call this page directly.'); |
||
| 12 | } |
||
| 13 | |||
| 14 | global $wpdb; |
||
| 15 | $this->fields = $wpdb->prefix . 'frm_fields'; |
||
| 16 | $this->forms = $wpdb->prefix . 'frm_forms'; |
||
| 17 | $this->entries = $wpdb->prefix . 'frm_items'; |
||
| 18 | $this->entry_metas = $wpdb->prefix . 'frm_item_metas'; |
||
| 19 | } |
||
| 20 | |||
| 21 | public function upgrade( $old_db_version = false ) { |
||
| 22 | global $wpdb; |
||
| 23 | //$frm_db_version is the version of the database we're moving to |
||
| 24 | $frm_db_version = FrmAppHelper::$db_version; |
||
| 25 | $old_db_version = (float) $old_db_version; |
||
| 26 | if ( ! $old_db_version ) { |
||
| 27 | $old_db_version = get_option('frm_db_version'); |
||
| 28 | } |
||
| 29 | |||
| 30 | if ( $frm_db_version != $old_db_version ) { |
||
| 31 | // update rewrite rules for views and other custom post types |
||
| 32 | flush_rewrite_rules(); |
||
| 33 | |||
| 34 | require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
||
| 35 | |||
| 36 | $this->create_tables(); |
||
| 37 | $this->migrate_data($frm_db_version, $old_db_version); |
||
| 38 | |||
| 39 | /***** SAVE DB VERSION *****/ |
||
| 40 | update_option('frm_db_version', $frm_db_version); |
||
| 41 | |||
| 42 | /**** ADD/UPDATE DEFAULT TEMPLATES ****/ |
||
| 43 | FrmXMLController::add_default_templates(); |
||
| 44 | } |
||
| 45 | |||
| 46 | do_action('frm_after_install'); |
||
| 47 | |||
| 48 | /**** update the styling settings ****/ |
||
| 49 | if ( is_admin() && function_exists( 'get_filesystem_method' ) ) { |
||
| 50 | $frm_style = new FrmStyle(); |
||
| 51 | $frm_style->update( 'default' ); |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | public function collation() { |
||
| 56 | global $wpdb; |
||
| 57 | if ( ! $wpdb->has_cap( 'collation' ) ) { |
||
| 58 | return ''; |
||
| 59 | } |
||
| 60 | |||
| 61 | $charset_collate = ''; |
||
| 62 | if ( ! empty( $wpdb->charset ) ) { |
||
| 63 | $charset_collate .= ' DEFAULT CHARACTER SET ' . $wpdb->charset; |
||
| 64 | } |
||
| 65 | |||
| 66 | if ( ! empty( $wpdb->collate ) ) { |
||
| 67 | $charset_collate .= ' COLLATE ' . $wpdb->collate; |
||
| 68 | } |
||
| 69 | |||
| 70 | return $charset_collate; |
||
| 71 | } |
||
| 72 | |||
| 73 | private function create_tables() { |
||
| 74 | $charset_collate = $this->collation(); |
||
| 75 | $sql = array(); |
||
| 76 | |||
| 77 | /* Create/Upgrade Fields Table */ |
||
| 78 | $sql[] = 'CREATE TABLE ' . $this->fields . ' ( |
||
| 79 | id int(11) NOT NULL auto_increment, |
||
| 80 | field_key varchar(100) default NULL, |
||
| 81 | name text default NULL, |
||
| 82 | description longtext default NULL, |
||
| 83 | type text default NULL, |
||
| 84 | default_value longtext default NULL, |
||
| 85 | options longtext default NULL, |
||
| 86 | field_order int(11) default 0, |
||
| 87 | required int(1) default NULL, |
||
| 88 | field_options longtext default NULL, |
||
| 89 | form_id int(11) default NULL, |
||
| 90 | created_at datetime NOT NULL, |
||
| 91 | PRIMARY KEY (id), |
||
| 92 | KEY form_id (form_id), |
||
| 93 | UNIQUE KEY field_key (field_key) |
||
| 94 | )'; |
||
| 95 | |||
| 96 | /* Create/Upgrade Forms Table */ |
||
| 97 | $sql[] = 'CREATE TABLE ' . $this->forms . ' ( |
||
| 98 | id int(11) NOT NULL auto_increment, |
||
| 99 | form_key varchar(100) default NULL, |
||
| 100 | name varchar(255) default NULL, |
||
| 101 | description text default NULL, |
||
| 102 | parent_form_id int(11) default 0, |
||
| 103 | logged_in tinyint(1) default NULL, |
||
| 104 | editable tinyint(1) default NULL, |
||
| 105 | is_template tinyint(1) default 0, |
||
| 106 | default_template tinyint(1) default 0, |
||
| 107 | status varchar(255) default NULL, |
||
| 108 | options longtext default NULL, |
||
| 109 | created_at datetime NOT NULL, |
||
| 110 | PRIMARY KEY (id), |
||
| 111 | UNIQUE KEY form_key (form_key) |
||
| 112 | )'; |
||
| 113 | |||
| 114 | /* Create/Upgrade Items Table */ |
||
| 115 | $sql[] = 'CREATE TABLE ' . $this->entries . ' ( |
||
| 116 | id int(11) NOT NULL auto_increment, |
||
| 117 | item_key varchar(100) default NULL, |
||
| 118 | name varchar(255) default NULL, |
||
| 119 | description text default NULL, |
||
| 120 | ip text default NULL, |
||
| 121 | form_id int(11) default NULL, |
||
| 122 | post_id int(11) default NULL, |
||
| 123 | user_id int(11) default NULL, |
||
| 124 | parent_item_id int(11) default 0, |
||
| 125 | is_draft tinyint(1) default 0, |
||
| 126 | updated_by int(11) default NULL, |
||
| 127 | created_at datetime NOT NULL, |
||
| 128 | updated_at datetime NOT NULL, |
||
| 129 | PRIMARY KEY (id), |
||
| 130 | KEY form_id (form_id), |
||
| 131 | KEY post_id (post_id), |
||
| 132 | KEY user_id (user_id), |
||
| 133 | KEY parent_item_id (parent_item_id), |
||
| 134 | UNIQUE KEY item_key (item_key) |
||
| 135 | )'; |
||
| 136 | |||
| 137 | /* Create/Upgrade Meta Table */ |
||
| 138 | $sql[] = 'CREATE TABLE ' . $this->entry_metas . ' ( |
||
| 139 | id int(11) NOT NULL auto_increment, |
||
| 140 | meta_value longtext default NULL, |
||
| 141 | field_id int(11) NOT NULL, |
||
| 142 | item_id int(11) NOT NULL, |
||
| 143 | created_at datetime NOT NULL, |
||
| 144 | PRIMARY KEY (id), |
||
| 145 | KEY field_id (field_id), |
||
| 146 | KEY item_id (item_id) |
||
| 147 | )'; |
||
| 148 | |||
| 149 | foreach ( $sql as $q ) { |
||
| 150 | if ( function_exists( 'dbDelta' ) ) { |
||
| 151 | dbDelta( $q . $charset_collate . ';' ); |
||
| 152 | } else { |
||
| 153 | global $wpdb; |
||
| 154 | $wpdb->query( $q . $charset_collate ); |
||
| 155 | } |
||
| 156 | unset($q); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param integer $frm_db_version |
||
| 162 | */ |
||
| 163 | private function migrate_data( $frm_db_version, $old_db_version ) { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Change array into format $wpdb->prepare can use |
||
| 175 | */ |
||
| 176 | public static function get_where_clause_and_values( &$args, $starts_with = ' WHERE ' ) { |
||
| 177 | if ( empty($args) ) { |
||
| 178 | // add an arg to prevent prepare from failing |
||
| 179 | $args = array( 'where' => $starts_with . '1=%d', 'values' => array( 1 ) ); |
||
| 180 | return; |
||
| 181 | } |
||
| 182 | |||
| 183 | $where = ''; |
||
| 184 | $values = array(); |
||
| 185 | |||
| 186 | if ( is_array( $args ) ) { |
||
| 187 | $base_where = $starts_with; |
||
| 188 | self::parse_where_from_array( $args, $base_where, $where, $values ); |
||
| 189 | } |
||
| 190 | |||
| 191 | $args = compact( 'where', 'values' ); |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param string $base_where |
||
| 196 | * @param string $where |
||
| 197 | */ |
||
| 198 | public static function parse_where_from_array( $args, $base_where, &$where, &$values ) { |
||
| 199 | $condition = ' AND'; |
||
| 200 | if ( isset( $args['or'] ) ) { |
||
| 201 | $condition = ' OR'; |
||
| 202 | unset( $args['or'] ); |
||
| 203 | } |
||
| 204 | |||
| 205 | foreach ( $args as $key => $value ) { |
||
| 206 | $where .= empty( $where ) ? $base_where : $condition; |
||
| 207 | $array_inc_null = ( ! is_numeric( $key ) && is_array( $value ) && in_array( null, $value ) ); |
||
| 208 | if ( is_numeric( $key ) || $array_inc_null ) { |
||
| 209 | $where .= ' ( '; |
||
| 210 | $nested_where = ''; |
||
| 211 | if ( $array_inc_null ) { |
||
| 212 | foreach ( $value as $val ) { |
||
| 213 | self::parse_where_from_array( array( $key => $val, 'or' => 1 ), '', $nested_where, $values ); |
||
| 214 | } |
||
| 215 | } else { |
||
| 216 | self::parse_where_from_array( $value, '', $nested_where, $values ); |
||
| 217 | } |
||
| 218 | $where .= $nested_where; |
||
| 219 | $where .= ' ) '; |
||
| 220 | } else { |
||
| 221 | self::interpret_array_to_sql( $key, $value, $where, $values ); |
||
| 222 | } |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param string $key |
||
| 228 | * @param string $where |
||
| 229 | */ |
||
| 230 | private static function interpret_array_to_sql( $key, $value, &$where, &$values ) { |
||
| 231 | $key = trim( $key ); |
||
| 232 | |||
| 233 | if ( strpos( $key, 'created_at' ) !== false || strpos( $key, 'updated_at' ) !== false ) { |
||
| 234 | $k = explode(' ', $key); |
||
| 235 | $where .= ' DATE_FORMAT(' . reset( $k ) . ', %s) ' . str_replace( reset( $k ), '', $key ); |
||
| 236 | $values[] = '%Y-%m-%d %H:%i:%s'; |
||
| 237 | } else { |
||
| 238 | $where .= ' ' . $key; |
||
| 239 | } |
||
| 240 | |||
| 241 | $lowercase_key = explode( ' ', strtolower( $key ) ); |
||
| 242 | $lowercase_key = end( $lowercase_key ); |
||
| 243 | |||
| 244 | if ( is_array( $value ) ) { |
||
| 245 | // translate array of values to "in" |
||
| 246 | if ( strpos( $lowercase_key, 'like' ) !== false ) { |
||
| 247 | $where = rtrim( $where, $key ); |
||
| 248 | $where .= '('; |
||
| 249 | $start = true; |
||
| 250 | foreach ( $value as $v ) { |
||
| 251 | if ( ! $start ) { |
||
| 252 | $where .= ' OR '; |
||
| 253 | } |
||
| 254 | $start = false; |
||
| 255 | $where .= $key . ' %s'; |
||
| 256 | $values[] = '%' . FrmAppHelper::esc_like( $v ) . '%'; |
||
| 257 | } |
||
| 258 | $where .= ')'; |
||
| 259 | } else if ( ! empty( $value ) ) { |
||
| 260 | $where .= ' in (' . FrmAppHelper::prepare_array_values( $value, '%s' ) . ')'; |
||
| 261 | $values = array_merge( $values, $value ); |
||
| 262 | } |
||
| 263 | } else if ( strpos( $lowercase_key, 'like' ) !== false ) { |
||
| 264 | /** |
||
| 265 | * Allow string to start or end with the value |
||
| 266 | * If the key is like% then skip the first % for starts with |
||
| 267 | * If the key is %like then skip the last % for ends with |
||
| 268 | */ |
||
| 269 | $start = $end = '%'; |
||
| 270 | if ( $lowercase_key == 'like%' ) { |
||
| 271 | $start = ''; |
||
| 272 | $where = rtrim( $where, '%' ); |
||
| 273 | } else if ( $lowercase_key == '%like' ) { |
||
| 274 | $end = ''; |
||
| 275 | $where = rtrim( rtrim( $where, '%like' ), '%LIKE' ); |
||
| 276 | $where .= 'like'; |
||
| 277 | } |
||
| 278 | |||
| 279 | $where .= ' %s'; |
||
| 280 | $values[] = $start . FrmAppHelper::esc_like( $value ) . $end; |
||
| 281 | |||
| 282 | } else if ( $value === null ) { |
||
| 283 | $where .= ' IS NULL'; |
||
| 284 | } else { |
||
| 285 | // allow a - to prevent = from being added |
||
| 286 | if ( substr( $key, -1 ) == '-' ) { |
||
| 287 | $where = rtrim( $where, '-' ); |
||
| 288 | } else { |
||
| 289 | $where .= '='; |
||
| 290 | } |
||
| 291 | |||
| 292 | $where .= is_numeric( $value ) ? ( strpos( $value, '.' ) !== false ? '%f' : '%d' ) : '%s'; |
||
| 293 | $values[] = $value; |
||
| 294 | } |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @param string $table |
||
| 299 | */ |
||
| 300 | public static function get_count( $table, $where = array(), $args = array() ) { |
||
| 304 | |||
| 305 | public static function get_var( $table, $where = array(), $field = 'id', $args = array(), $limit = '', $type = 'var' ) { |
||
| 306 | $group = ''; |
||
| 307 | self::get_group_and_table_name( $table, $group ); |
||
| 308 | self::convert_options_to_array( $args, '', $limit ); |
||
| 309 | |||
| 310 | $query = 'SELECT ' . $field . ' FROM ' . $table; |
||
| 311 | if ( is_array( $where ) || empty( $where ) ) { |
||
| 312 | // only separate into array values and query string if is array |
||
| 313 | self::get_where_clause_and_values( $where ); |
||
| 314 | global $wpdb; |
||
| 315 | $query = $wpdb->prepare( $query . $where['where'] . ' ' . implode( ' ', $args ), $where['values'] ); |
||
| 316 | } else { |
||
| 317 | /** |
||
| 318 | * Allow the $where to be prepared before we recieve it here. |
||
| 319 | * This is a fallback for reverse compatability, but is not recommended |
||
| 320 | */ |
||
| 321 | _deprecated_argument( 'where', '2.0', __( 'Use the query in an array format so it can be properly prepared.', 'formidable' ) ); |
||
| 322 | $query .= $where . ' ' . implode( ' ', $args ); |
||
| 323 | } |
||
| 324 | |||
| 325 | $cache_key = str_replace( array( ' ', ',' ), '_', trim( implode( '_', FrmAppHelper::array_flatten( $where ) ) . implode( '_', $args ) . $field . '_' . $type, ' WHERE' ) ); |
||
| 326 | $results = FrmAppHelper::check_cache( $cache_key, $group, $query, 'get_' . $type ); |
||
| 327 | return $results; |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @param string $table |
||
| 332 | * @param array $where |
||
| 333 | */ |
||
| 334 | public static function get_col( $table, $where = array(), $field = 'id', $args = array(), $limit = '' ) { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @since 2.0 |
||
| 340 | * @param string $table |
||
| 341 | */ |
||
| 342 | public static function get_row( $table, $where = array(), $fields = '*', $args = array() ) { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @param string $table |
||
| 349 | */ |
||
| 350 | public static function get_one_record( $table, $args = array(), $fields = '*', $order_by = '' ) { |
||
| 354 | |||
| 355 | public static function get_records( $table, $args = array(), $order_by = '', $limit = '', $fields = '*' ) { |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Prepare a key/value array before DB call |
||
| 362 | * @since 2.0 |
||
| 363 | * @param string $table |
||
| 364 | */ |
||
| 365 | public static function get_results( $table, $where = array(), $fields = '*', $args = array() ) { |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Check for like, not like, in, not in, =, !=, >, <, <=, >= |
||
| 371 | * Return a value to append to the where array key |
||
| 372 | * |
||
| 373 | * @return string |
||
| 374 | */ |
||
| 375 | public static function append_where_is( $where_is ) { |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Get 'frm_forms' from wp_frm_forms or a longer table param that includes a join |
||
| 405 | * Also add the wpdb->prefix to the table if it's missing |
||
| 406 | * |
||
| 407 | * @param string $table |
||
| 408 | * @param string $group |
||
| 409 | */ |
||
| 410 | private static function get_group_and_table_name( &$table, &$group ) { |
||
| 427 | |||
| 428 | private static function convert_options_to_array( &$args, $order_by = '', $limit = '' ) { |
||
| 429 | if ( ! is_array($args) ) { |
||
| 430 | $args = array( 'order_by' => $args ); |
||
| 431 | } |
||
| 464 | |||
| 465 | public function uninstall() { |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Migrate old styling settings. If sites are using the old |
||
| 519 | * default 400px field width, switch it to 100% |
||
| 520 | * |
||
| 521 | * @since 2.0.4 |
||
| 522 | */ |
||
| 523 | private function migrate_to_25() { |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Check if the parent_form_id columns exists. |
||
| 542 | * If not, try and add it again |
||
| 543 | * |
||
| 544 | * @since 2.0.2 |
||
| 545 | */ |
||
| 546 | private function migrate_to_23() { |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Change field size from character to pixel -- Multiply by 9 |
||
| 556 | */ |
||
| 557 | private function migrate_to_17() { |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Migrate post and email notification settings into actions |
||
| 604 | */ |
||
| 605 | private function migrate_to_16() { |
||
| 654 | |||
| 655 | private function migrate_to_11() { |
||
| 690 | |||
| 691 | private function migrate_to_6() { |
||
| 728 | |||
| 729 | private function migrate_to_4() { |
||
| 736 | } |
||
| 737 |