/**
  * Parse the container for links and save the results to the DB.
  *
  * @return void
  */
 function synch()
 {
     //FB::log("Parsing {$this->container_type}[{$this->container_id}]");
     //Remove any existing link instance records associated with the container
     $this->delete_instances();
     //Load the wrapped object, if not done already
     $this->get_wrapped_object();
     //FB::log($this->fields, "Parseable fields :");
     //Iterate over all parse-able fields
     foreach ($this->fields as $name => $format) {
         //Get the field value
         $value = $this->get_field($name);
         if (empty($value)) {
             //FB::log($name, "Skipping empty field");
             continue;
         }
         //FB::log($name, "Parsing field");
         //Get all parsers applicable to this field
         $parsers = blcParserHelper::get_parsers($format, $this->container_type);
         //FB::log($parsers, "Applicable parsers");
         if (empty($parsers)) {
             continue;
         }
         $base_url = $this->base_url();
         $default_link_text = $this->default_link_text($name);
         //Parse the field with each parser
         foreach ($parsers as $parser) {
             //FB::log("Parsing $name with '{$parser->parser_type}' parser");
             $found_instances = $parser->parse($value, $base_url, $default_link_text);
             //FB::log($found_instances, "Found instances");
             $transactionManager = TransactionManager::getInstance();
             $transactionManager->start();
             //Complete the link instances by adding container info, then save them to the DB.
             foreach ($found_instances as $instance) {
                 $instance->set_container($this, $name);
                 $instance->save();
             }
             $transactionManager->commit();
         }
     }
     $this->mark_as_synched();
 }
 /**
  * Get the parser associated with this link instance.
  *
  * @return blcParser|null
  */
 function &get_parser()
 {
     if (is_null($this->_parser)) {
         $this->_parser =& blcParserHelper::get_parser($this->parser_type);
     }
     return $this->_parser;
 }
 /**
  * Hook for the 'the_content' filter. Scans the current post and adds the 'broken_link' 
  * CSS class to all links that are known to be broken. Currently works only on standard
  * HTML links (i.e. the '<a href=...' kind). 
  *
  * @param string $content Post content
  * @return string Modified post content.
  */
 function hook_the_content($content)
 {
     global $post, $wpdb;
     /** @var wpdb $wpdb */
     if (empty($post) || !in_array($post->post_type, $this->enabled_post_types)) {
         return $content;
     }
     //Retrieve info about all occurences of broken links in the current post
     $q = "\n\t\t\tSELECT instances.raw_url\n\t\t\tFROM {$wpdb->prefix}blc_instances AS instances JOIN {$wpdb->prefix}blc_links AS links \n\t\t\t\tON instances.link_id = links.link_id\n\t\t\tWHERE \n\t\t\t\tinstances.container_type = %s\n\t\t\t\tAND instances.container_id = %d\n\t\t\t\tAND links.broken = 1\n\t\t\t\tAND parser_type = 'link' \n\t\t";
     $q = $wpdb->prepare($q, $post->post_type, $post->ID);
     $links = $wpdb->get_results($q, ARRAY_A);
     //Return the content unmodified if there are no broken links in this post.
     if (empty($links) || !is_array($links)) {
         return $content;
     }
     //Put the broken link URLs in an array
     $broken_link_urls = array();
     foreach ($links as $link) {
         $broken_link_urls[] = $link['raw_url'];
     }
     //Iterate over all HTML links and modify the broken ones
     if ($parser = blcParserHelper::get_parser('link')) {
         $content = $parser->multi_edit($content, array(&$this, 'highlight_broken_link'), $broken_link_urls);
     }
     return $content;
 }