Update: use variables instead of the database table to store node type settings

see: https://drupal.org/node/2101573

diff --git a/includes/pages.inc b/includes/pages.inc
index be45017..bd93cee 100644
--- a/includes/pages.inc
+++ b/includes/pages.inc
@@ -20,9 +20,10 @@ function node_accessibility_accessibility_tab_page($form, &$form_state, $node, $
   $database = FALSE;
   $perform_validation = FALSE;
 
-  $vids = array();
-
+  $node_type_settings = node_accessibility_load_node_type_settings();
+  $node_settings = isset($node_type_settings[$node->type]) ? $node_type_settings[$node->type] : FALSE;
 
+  $vids = array();
   if ($vid == NULL) {
     if (module_exists('workbench_moderation') && _workbench_moderation_access_current_draft($node)) {
       if (property_exists($node, 'workbench_moderation') && isset($node->workbench_moderation['current'])) {
@@ -39,14 +40,14 @@ function node_accessibility_accessibility_tab_page($form, &$form_state, $node, $
     $perform_validation = TRUE;
   }
 
-  if (property_exists($node, 'accessibility') && is_array($node->accessibility)) {
-    if (!empty($node->accessibility['format'])) {
-      $markup_format = $node->accessibility['format'];
+  if (is_array($node_settings)) {
+    if (!empty($node_settings['format'])) {
+      $markup_format = $node_settings['format'];
     }
 
-    if (!empty($node->accessibility['method']) && !empty($methods[$node->accessibility['method']])) {
-      $automatic = $methods[$node->accessibility['method']]['automatic'];
-      $database = $methods[$node->accessibility['method']]['database'];
+    if (!empty($node_settings['method']) && !empty($methods[$node_settings['method']])) {
+      $automatic = $methods[$node_settings['method']]['automatic'];
+      $database = $methods[$node_settings['method']]['database'];
     }
   }
 
diff --git a/node_accessibility.install b/node_accessibility.install
index ca4f32d..892a704 100644
--- a/node_accessibility.install
+++ b/node_accessibility.install
@@ -71,59 +71,6 @@ function node_accessibility_schema() {
     ),
   );
 
-  $schema['node_accessibility_types'] = array(
-    'description' => t("Content type specific settings for quail api."),
-    'fields' => array(
-      'type' => array(
-        'description' => t("The machine-readable name of the node type that has accessibility validation enabled."),
-        'type' => 'varchar',
-        'length' => 32,
-        'not null' => TRUE,
-        'default' => '',
-      ),
-      'required' => array(
-        'description' => t("A boolean that designates whether or not accessibility validation is required."),
-        'type' => 'int',
-        'size' => 'tiny',
-        'unsigned' => TRUE,
-        'not null' => TRUE,
-        'default' => TRUE,
-      ),
-      'standards' => array(
-        'description' => t("A serialized array that contains the machine name of all enabled standards."),
-        'type' => 'blob',
-        'size' => 'big',
-        'not null' => FALSE,
-        'serialize' => TRUE,
-      ),
-      'method' => array(
-        'description' => t("A string that represent the validation method to use."),
-        'type' => 'varchar',
-        'length' => 64,
-        'not null' => TRUE,
-        'default' => '',
-      ),
-      'format' => array(
-        'description' => t("The filter format to use when providing validation results."),
-        'type' => 'varchar',
-        'length' => 255,
-        'not null' => TRUE,
-        'default' => '',
-      ),
-    ),
-    'primary key' => array('type'),
-    'foreign keys' => array(
-      'type' => array(
-        'table' => 'node_type',
-        'columns' => array('type' => 'type'),
-      ),
-      'format' => array(
-        'table' => 'filter_format',
-        'columns' => array('format' => 'format'),
-      ),
-    ),
-  );
-
   // workaround mysql's violation of the SQL standard in a way that does not break standards-compliant databases.
   // @see https://dev.mysql.com/doc/refman/5.6/en/data-type-defaults.html
   // @see https://bugs.mysql.com/bug.php?id=25520
@@ -140,12 +87,62 @@ function node_accessibility_schema() {
  * Implementation of hook_install().
  */
 function node_accessibility_install() {
-  variable_set('node_accessibility_alter_revision_menu', TRUE);
+
+  if (function_exists('cf_settings_register')) {
+    cf_settings_register('node_accessibility_node_type_settings', 'drupal_variables', 'node_accessibility', array());
+    cf_settings_register('node_accessibility_alter_revision_menu', 'drupal_variables', 'node_accessibility', TRUE);
+  }
+  else {
+    variable_set('node_accessibility_node_type_settings', array());
+    variable_set('node_accessibility_alter_revision_menu', TRUE);
+  }
 }
 
 /**
  * Implementation of hook_uninstall().
  */
 function node_accessibility_uninstall() {
-  variable_del('node_accessibility_alter_revision_menu');
+  if (function_exists('cf_settings_unregister')) {
+    $registered = cf_settings_get_registered(array('module_name' => 'node_accessibility'), 'id');
+
+    foreach ($registered as &$r) {
+      cf_settings_unregister($r->variable_name, $r->variable_type, $r->module);
+    }
+  }
+  else {
+    variable_del('node_accessibility_node_type_settings');
+    variable_del('node_accessibility_alter_revision_menu');
+  }
+}
+
+/**
+ * Switch to variable_set()/variable_get() node type settings.
+ *
+ * If enabled, cf_settings will now be utilized.
+ *
+ * see: https://drupal.org/node/2101573
+ */
+function node_accessibility_update_7100() {
+  $query = db_select('node_accessibility_types', 'nat');
+  $query->fields('nat');
+  $query->orderBy('nat.type', 'ASC');
+
+  $node_type_settings = array();
+  $results = (array) $query->execute()->fetchAll();
+  foreach ($results as $result) {
+    $node_type_settings[$result->type] = array();
+    $node_type_settings[$result->type]['required'] = $result->required;
+    $node_type_settings[$result->type]['standards'] = unserialize($result->standards);
+    $node_type_settings[$result->type]['method'] = $result->method;
+    $node_type_settings[$result->type]['format'] = $result->format;
+  }
+
+  if (function_exists('cf_settings_register')) {
+    cf_settings_register('node_accessibility_node_type_settings', 'drupal_variables', 'node_accessibility', array());
+    cf_settings_register('node_accessibility_alter_revision_menu', 'drupal_variables', 'node_accessibility', TRUE);
+  }
+
+  variable_set('node_accessibility_node_type_settings', $node_type_settings);
+
+  db_drop_table('node_accessibility_types');
 }
diff --git a/node_accessibility.module b/node_accessibility.module
index d6a6236..d7f1959 100644
--- a/node_accessibility.module
+++ b/node_accessibility.module
@@ -33,8 +33,6 @@ function node_accessibility_quail_api_permission_alter(&$permissions) {
     'title' => t("Perform node accessibility validation"),
     'description' => t("Grants permissions to use the perform a manual validation of any given node."),
   );
-
-  // @todo: add node-specific validation permissions or at the very least node_type-specific
 }
 
 /**
@@ -270,15 +268,14 @@ function node_accessibility_form_node_type_form_alter(&$form, &$form_state, $for
   $default_format = 'full_html';
 
   if (!empty($form['#node_type']->type)) {
-    $node_type_settings_objects = node_accessibility_load_node_type_settings(array($form['#node_type']), NULL);
-
-    if (!empty($node_type_settings_objects)) {
-      $node_type_settings_object = $node_type_settings_objects['0'];
+    $node_type = $form['#node_type']->type;
+    $node_type_settings = node_accessibility_load_node_type_settings();
 
-      $default_enabled = ($node_type_settings_object->required ? 'required' : 'optional');
-      $default_standards = unserialize($node_type_settings_object->standards);
-      $default_method = (!empty($node_type_settings_object->method) ? $node_type_settings_object->method : $default_method);
-      $default_format = (!empty($node_type_settings_object->format) ? $node_type_settings_object->format : $default_format);
+    if (isset($node_type_settings[$node_type])) {
+      $default_enabled = isset($node_type_settings[$node_type]['required']) && $node_type_settings[$node_type]['required'] ? 'required' : 'optional';
+      $default_standards = isset($node_type_settings[$node_type]['standards']) ? $node_type_settings[$node_type]['standards'] : $default_standards;
+      $default_method = isset($node_type_settings[$node_type]['method']) ? $node_type_settings[$node_type]['method'] : $default_method;
+      $default_format = isset($node_type_settings[$node_type]['format']) ? $node_type_settings[$node_type]['format'] : $default_format;
     }
   }
 
@@ -356,12 +353,7 @@ function node_accessibility_node_type_form_submit($form, &$form_state) {
     }
   }
 
-  $node_type_settings_objects = node_accessibility_load_node_type_settings(array($form_state['values']['type']), NULL);
-  $node_type_settings_object = FALSE;
-
-  if (!empty($node_type_settings_objects)) {
-    $node_type_settings_object = $node_type_settings_objects['0'];
-  }
+  $node_type_settings = node_accessibility_load_node_type_settings();
 
   $columns = array('enabled', 'required', 'standards', 'method', 'format');
   $record = array();
@@ -388,13 +380,8 @@ function node_accessibility_node_type_form_submit($form, &$form_state) {
     $record['standards'] = array();
   }
 
-  if (is_object($node_type_settings_object)) {
-    $primary_keys = array('type');
-    $results = drupal_write_record('node_accessibility_types', $record, $primary_keys);
-  }
-  else {
-    $results = drupal_write_record('node_accessibility_types', $record);
-  }
+  $node_type_settings[$form_state['values']['type']] = $record;
+  variable_set('node_accessibility_node_type_settings', $node_type_settings);
 }
 
 /**
@@ -422,43 +409,21 @@ function node_accessibility_node_operations() {
  */
 function node_accessibility_operation_validate($nids) {
   $nodes = node_load_multiple($nids);
-  $node_type_settings_objects = node_accessibility_load_node_type_settings($nodes, NULL);
-  $node_type_settings_types = array();
-
-  foreach ($node_type_settings_objects as $node_type_settings_object) {
-    if (is_object($node_type_settings_object)) {
-      $node_type_settings_types[$node_type_settings_object->type] = $node_type_settings_object;
-    }
-  }
+  $node_type_settings = node_accessibility_load_node_type_settings();
 
-  unset($node_type_settings_objects);
   $result = TRUE;
 
   foreach ($nodes as $key => $node) {
     if (!is_object($node)) continue;
-    if (!array_key_exists($node->type, $node_type_settings_types)) continue;
+    if (!array_key_exists($node->type, $node_type_settings)) continue;
+    if ($node_type_settings[$node->type] === FALSE) continue;
 
     $methods = quail_api_get_validation_methods(NULL);
     $database = FALSE;
-    $node_settings = $node_type_settings_types[$node->type];
-
-    if (!empty($node_settings->method) && is_array($methods) && array_key_exists('database', $methods[$node_settings->method])) {
-      $database = $methods[$node_settings->method]['database'];
-    }
-
-    // @fixme perhaps this is a caching issue, but the node->accessibility settings is not always accurate
-    // manually create the accessibility array because it must already be enabled for the code to get this far
-    if (!property_exists($node, 'accessibility') || !is_array($node->accessibility)) {
-      $node->accessibility = array();
+    $node_settings = $node_type_settings[$node->type];
 
-      foreach ($node_settings as $key => $value) {
-        if ($key == 'standards') {
-          $node->accessibility[$key] = unserialize($value);
-        }
-        else {
-          $node->accessibility[$key] = $value;
-        }
-      }
+    if (!empty($node_settings['method']) && is_array($methods) && array_key_exists('database', $methods[$node_settings['method']])) {
+      $database = $methods[$node_settings['method']]['database'];
     }
 
     $results = node_accessibility_perform_validation(array($node), array(), NULL, NULL);
@@ -539,6 +504,9 @@ function node_accessibility_perform_validation($nodes_or_nids, $vids = array(),
     return array();
   }
 
+  $node_type_settings = node_accessibility_load_node_type_settings();
+
+
   $results = array();
   $standards = quail_api_get_standards(NULL, 'snippet');
 
@@ -558,6 +526,11 @@ function node_accessibility_perform_validation($nodes_or_nids, $vids = array(),
       }
     }
 
+    $node_settings = FALSE;
+    if (isset($node_type_settings[$node->type])) {
+      $node_settings = $node_type_settings[$node->type];
+    }
+
     if (array_key_exists($node->nid, $vids) && !empty($vids[$node->nid])) {
       $revisions = $vids[$node->nid];
     }
@@ -585,35 +558,13 @@ function node_accessibility_perform_validation($nodes_or_nids, $vids = array(),
         }
       }
 
-      // manually load accessibility settings if not currently loaded.
-      if (!property_exists($revision, 'accessibility')) {
-        $revision->accessibility = FALSE;
-        $type_settings = node_accessibility_load_node_type_settings(array($revision->nid => $revision), NULL);
-
-        if (!empty($type_settings)) {
-          $revision->accessibility = array();
-
-          reset($type_settings);
-          $type_setting = current($type_settings);
-
-          foreach ($type_setting as $key => $value) {
-            if ($key == 'standards') {
-              $revision->accessibility[$key] = unserialize($value);
-            }
-            else {
-              $revision->accessibility[$key] = $value;
-            }
-          }
-        }
-      }
-
-      if (is_array($revision->accessibility)) {
+      if (is_array($node_settings)) {
         $node_view = node_view($revision, 'full', $language);
         $rendered_node = drupal_render($node_view);
         unset($node_view);
 
-        if (!empty($revision->accessibility['standards'])) {
-          foreach ($revision->accessibility['standards'] as $standard_name) {
+        if (!empty($node_settings['standards'])) {
+          foreach ($node_settings['standards'] as $standard_name) {
             $results[$revision->nid][$rid] = array_merge($results[$revision->nid][$rid], quail_api_validate_markup($rendered_node, $standards[$standard_name], $display_level));
 
             if (module_exists('rules')) {
@@ -629,125 +580,19 @@ function node_accessibility_perform_validation($nodes_or_nids, $vids = array(),
 }
 
 /**
- * Implements hook_node_load().
- */
-function node_accessibility_node_load($nodes, $types) {
-  $all_nodes = array();
-
-  foreach ($nodes as &$node) {
-    $all_nodes[$node->nid] = $node;
-    $all_nodes[$node->nid]->accessibility = FALSE;
-  }
-
-  $results = node_accessibility_load_node_type_settings($all_nodes, NULL);
-
-  foreach ($results as $result) {
-    $all_nodes[$node->nid]->accessibility = array();
-
-    foreach ($result as $key => $value) {
-      if ($key == 'standards') {
-        $all_nodes[$node->nid]->accessibility[$key] = unserialize($value);
-      }
-      else {
-        $all_nodes[$node->nid]->accessibility[$key] = $value;
-      }
-    }
-  }
-
-  return $all_nodes;
-}
-
-/**
  * Loads the node type settings table data for the given node type.
  *
- * @todo add caching support to the results of this check.
- *
- * @param array $node_types
- *   (optional) An array of node type strings or node objects.
- * @param string|null $keyed
- *   (optional) A string matching one of the following: 'type'
- *   When this is NULL, the default behavior is to return the array exactly as
- *   it was returned by the database call.
- *   When this is a valid string, the key names of the returned array will use
- *   the specified key name.
- *
  * @return array
- *   A prepared statement object, already executed.
+ *   An array of values returned by variable_get().
  */
-function node_accessibility_load_node_type_settings($node_types = array(), $keyed = NULL) {
-  if (!is_array($node_types)) {
-    if (class_exists('cf_error')) {
-      cf_error::invalid_array('node_types');
-    }
+function node_accessibility_load_node_type_settings() {
+  static $node_accessibility_node_type_settings;
 
-    return array();
-  }
-
-  $query = db_select('node_accessibility_types', 'nat');
-
-  $query->fields('nat');
-  $query->orderBy('nat.type', 'ASC');
-
-  $total_types = count($node_types);
-
-  if ($total_types > 1) {
-    $or = db_or();
-
-    foreach ($node_types as $node_type) {
-      if (is_object($node_type)) {
-        $or->condition('type', $node_type->type, '=');
-      }
-      else {
-        $or->condition('type', $node_type, '=');
-      }
-    }
-
-    $query->condition($or);
-  }
-  else if ($total_types == 1) {
-    $node_type = array_pop($node_types);
-
-    if (is_object($node_type)) {
-      $query->condition('type', $node_type->type, '=');
-    }
-    else {
-      $query->condition('type', $node_type, '=');
-    }
+  if (!isset($node_accessibility_node_type_settings)) {
+    $node_accessibility_node_type_settings = variable_get('node_accessibility_node_type_settings', array());
   }
 
-  if ($keyed === 'type') {
-    $results = array();
-
-    try {
-      $records = $query->execute();
-    }
-    catch (Exception $e) {
-      if (class_exists('cf_error')) {
-        cf_error::on_query_execution($e);
-      }
-
-      return array();
-    }
-
-    foreach ($records as $record) {
-      if (!is_object($record)) continue;
-
-      $results[$record->$keyed] = $record;
-    }
-
-    return $results;
-  }
-
-  try {
-    return $query->execute()->fetchAll();
-  }
-  catch (Exception $e) {
-    if (class_exists('cf_error')) {
-      cf_error::on_query_execution($e);
-    }
-  }
-
-  return array();
+  return $node_accessibility_node_type_settings;
 }
 
 /**
@@ -893,18 +738,17 @@ function node_accessibility_delete_node_problems($nid, $vid = NULL) {
 /**
  * Returns TRUE if accessibility validation functionality is enabled.
  *
- * @todo add caching support to the results of this check.
- *
  * @param string $node_type
  *   A node type string.
  *
- * @return int|false
- *   The return states of either FALSE, SAVED_NEW, or SAVED_UPDATED.
+ * @return bool
+ *   The return TRUE when accessibility is enabled for the given node type.
+ *   Otherwise, FALSE is returned.
  */
 function node_accessibility_is_enabled($node_type) {
-  $node_type_settings_objects = node_accessibility_load_node_type_settings(array($node_type), NULL);
+  $node_type_settings = node_accessibility_load_node_type_settings();
 
-  if (!empty($node_type_settings_objects) && is_object($node_type_settings_objects['0'])) {
+  if (isset($node_type_settings[$node_type]) && $node_type_settings[$node_type] !== FALSE) {
     return TRUE;
   }
 
@@ -914,8 +758,6 @@ function node_accessibility_is_enabled($node_type) {
 /**
  * Returns TRUE if accessibility validation functionality is required.
  *
- * @todo add caching support to the results of this check.
- *
  * @param string $node_type
  *   A node type string
  *
@@ -925,14 +767,13 @@ function node_accessibility_is_enabled($node_type) {
  *   NULL is returned if accessibility validation functionality is not enabled.
  */
 function node_accessibility_is_required($node_type) {
-  $node_type_settings_objects = node_accessibility_load_node_type_settings(array($node_type), NULL);
+  $node_type_settings = node_accessibility_load_node_type_settings();
 
-  if (empty($node_type_settings_objects) || !is_object($node_type_settings_objects['0'])) {
+  if (!isset($node_type_settings[$node_type]) || $node_type_settings[$node_type] === FALSE) {
     return NULL;
   }
 
-  // @fixme: Is this the propery way? I did this off the top of my mind and I could be doing something wrong here.
-  if ($node_type_settings_objects['0']->required == TRUE) {
+  if (isset($node_type_settings[$node_type]['required']) && $node_type_settings[$node_type]['required'] === TRUE) {
     return TRUE;
   }
 
@@ -1376,10 +1217,13 @@ function node_accessibility_validate_action($node) {
   $results = node_accessibility_perform_validation(array($node));
   $reports = array();
 
+  $node_type_settings = node_accessibility_load_node_type_settings();
+  $node_settings = $node_type_settings[$node->type];
+
   if (isset($results[$node->nid][$node->vid]['report'])) {
     $reports = $results[$node->nid][$node->vid]['report'];
     $methods = quail_api_get_validation_methods(NULL);
-    $database = isset($methods[$node->accessibility['method']]['database']) ? $methods[$node->accessibility['method']]['database'] : FALSE;
+    $database = isset($methods[$node_settings['method']]['database']) ? $methods[$node_settings['method']]['database'] : FALSE;
     unset($results);
 
     if ($database && !empty($reports)) {
