prefix . self::$tableBase; if ( ! self::checkIsTableExists( $table ) ) { $charset_collate = ''; if ( ! empty ( $wpdb->charset ) ) { $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}"; } if ( ! empty ( $wpdb->collate ) ) { $charset_collate .= " COLLATE {$wpdb->collate}"; } $sql = "CREATE TABLE $table ( id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, object_type VARCHAR(20) NOT NULL, object_id BIGINT(20) UNSIGNED NOT NULL, date_add DATETIME, PRIMARY KEY (id) ) $charset_collate;"; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta( $sql ); if ( self::checkIsTableExists( $table ) ) { return true; } return false; } return true; } public static function checkIsTableExists( $table ) { global $wpdb; return $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ) === $table; } /** * Check object type * * @param $type * * @return bool */ public static function isAllowedObjectType( $type ) { return in_array( $type, self::$allowedObjectTypes, true ); } /** * Add row * * @param string $object_type * @param int $object_id * @param string|null $date_add * * @return int Inserted row id */ public static function add( $object_type, $object_id, $date_add = null ) { global $wpdb; if ( ! self::isAllowedObjectType( $object_type ) ) { return false; } if ( ! Helper::check_id_is_valid( $object_id ) ) { return false; } if ( ! self::maybeCreateTable() ) { return false; } if ( ! $date_add ) { $date_add = gmdate( 'Y-m-d H:i:s' ); } $table = $wpdb->prefix . self::$tableBase; $data = [ 'object_type' => $object_type, 'object_id' => $object_id, 'date_add' => $date_add ]; $format = [ '%s', '%d', '%s' ]; $wpdb->insert( $table, $data, $format ); return $wpdb->insert_id; } /** * Get rows * * @param int $after * @param int $limit * * @return array|bool Array of result objects or false on error */ public static function get( $after, $limit ) { global $wpdb; if ( ! self::maybeCreateTable() ) { return false; } $rows_arr = false; if ( ! $after && ! $limit ) { $rows_arr = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}adscale_entity_changes ORDER BY `id` ASC", OBJECT ); } if ( $after && ! $limit ) { $rows_arr = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}adscale_entity_changes WHERE `id` > %d ORDER BY `id` ASC", $after ), OBJECT ); } if ( ! $after && $limit ) { $rows_arr = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}adscale_entity_changes ORDER BY `id` ASC LIMIT %d", $limit ), OBJECT ); } if ( $after && $limit ) { $rows_arr = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}adscale_entity_changes WHERE `id` > %d ORDER BY `id` ASC LIMIT %d", $after, $limit ), OBJECT ); } if ( ! is_array( $rows_arr ) ) { return false; } return $rows_arr; } /** * Count rows by object type and id * * @param string $object_type * @param int $object_id * * @return bool|int Int with count of rows on success or false on error */ public static function countByTypeAndObjectId( $object_type, $object_id ) { global $wpdb; if ( ! self::isAllowedObjectType( $object_type ) ) { return false; } if ( ! Helper::check_id_is_valid( $object_id ) ) { return false; } if ( ! self::maybeCreateTable() ) { return false; } $result = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}adscale_entity_changes WHERE object_type = %s AND object_id = %d", $object_type, $object_id ) ); if ( is_numeric( $result ) ) { return (int) $result; } return false; } /** * Delete rows by max id * * @param int $max_id * * @return int|bool Number of rows affected. Boolean false on error. */ public static function deleteManyByMaxId( $max_id ) { global $wpdb; if ( ! Helper::check_id_is_valid( $max_id ) ) { return false; } if ( ! self::maybeCreateTable() ) { return false; } $result = $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}adscale_entity_changes WHERE id <= %d", $max_id ) ); return is_numeric( $result ); } }