Drupal 7: Attach JS With Conditions

In the code below, I leverage PHP's strpos function which finds the first occurrence of a substring in a string. In this case, we want to find if a specific HTML class appears in code rendered by a wysiwyg editor. The class is video-filter and if that class appears, add fitvids.js.

In addition, I create an array of content types as well as defining which Drupal field we want to search in.

 * Override or insert variables into the node template.
function template_preprocess_node(&$vars) {

  // Add fitvids to content types that have the
  // video-filter class in the body field.
  // Define an array of content types.
  $video_types = array(

  // Define the target field.
  $field_names = array(

  // Check to see if the node is not empty and a "full" view mode.
  if (in_array($vars['view_mode'], array('full'))) {
    // Check for empty nodes.
    if (!empty($node)) {
      $type = $node->type;

    // Check for the array of content types using the "in_array" function.
    // Then see if the class 'video-filter' is whithin the body field within those types
    // using the PHP needle in haystack function.
    if (!empty($type) && in_array($type, $video_types)) {
      foreach ($field_names as $field_name) {
        if (
          isset($vars['content'][$field_name][0]['#markup']) &&
          strpos($vars['content'][$field_name][0]['#markup'], 'video-filter') !== FALSE
        ) {
          $vars['content'][$field_name]['#attached']['js'][] = $theme_path . '/js/jquery.fitvids.min.js';