/**
  * Process links in content
  * @global obj $wpdb DB instance
  * @global obj $post Current post
  * @param string $content Text containing links
  * @param string (optional) $group Group to add links to (Default: none)
  * @return string Content with processed links 
  */
 function process_links($content, $group = null)
 {
     //Validate
     if (!is_string($content) || empty($content) || !!$this->widget_processing && !$this->options->get_bool('enabled_widget')) {
         return $content;
     }
     //Extract links
     $links = $this->get_links($content, true);
     //Do not process content without links
     if (empty($links)) {
         return $content;
     }
     //Process links
     $protocol = array('http://', 'https://');
     $uri_home = strtolower(home_url());
     $domain = str_replace($protocol, '', $uri_home);
     $qv_att = 'attachment_id';
     //Setup group properties
     $g_props = (object) array('enabled' => $this->options->get_bool('group_links'), 'attr' => 'group', 'base' => '', 'legacy_prefix' => 'lightbox[', 'legacy_suffix' => ']');
     if ($g_props->enabled) {
         $g_props->base = is_scalar($group) ? trim(strval($group)) : '';
     }
     //Initialize content handlers
     if (!$this->handlers instanceof SLB_Content_Handlers) {
         $this->handlers = new SLB_Content_Handlers($this);
     }
     //Iterate through and activate supported links
     $uri_proto = array('raw' => '', 'source' => '');
     foreach ($links as $link) {
         //Init vars
         $pid = 0;
         $link_new = $link;
         $internal = false;
         $q = null;
         $uri = (object) $uri_proto;
         $type = false;
         $props_extra = array();
         //Parse link attributes
         $attrs = $this->util->parse_attribute_string($link_new, array('href' => ''));
         //Get URI
         $uri->raw = $uri->source = $attrs['href'];
         //Stop processing invalid links
         if (!$this->validate_uri($uri->raw) || $this->has_attribute($attrs, 'active', false)) {
             continue;
         }
         //Check if item links to internal media (attachment)
         if (0 === strpos($uri->raw, '/')) {
             //Relative URIs are always internal
             $internal = true;
             $uri->source = $uri_home . $uri->raw;
         } else {
             //Absolute URI
             $uri_dom = str_replace($protocol, '', strtolower($uri->raw));
             if (strpos($uri_dom, $domain) === 0) {
                 $internal = true;
             }
             unset($uri_dom);
         }
         //Get source URI (e.g. attachments)
         if ($internal && is_local_attachment($uri->source)) {
             $pid = url_to_postid($uri->source);
             $src = wp_get_attachment_url($pid);
             if (!!$src) {
                 $uri->source = $src;
                 $props_extra['id'] = $pid;
             }
             unset($src);
         }
         /* Determine content type */
         //Check if URI has already been processed
         if ($this->media_item_cached($uri->source)) {
             $i = $this->get_cached_media_item($uri->source);
             $type = $i->type;
         } else {
             //Get handler match
             $hdl_result = $this->handlers->match($uri->source);
             if (!!$hdl_result->handler) {
                 $type = $hdl_result->handler->get_id();
                 $props_extra = $hdl_result->props;
                 //Updated source URI
                 if (isset($props_extra['uri'])) {
                     $uri->source = $props_extra['uri'];
                     unset($props_extra['uri']);
                 }
             }
         }
         //Stop processing if link type not valid
         if (!$type) {
             //Cache
             $this->validated_uris[$uri->raw] = false;
             continue;
         }
         //Set group (if enabled)
         if ($g_props->enabled) {
             $group = array();
             //Get preset group attribute
             $g = $this->has_attribute($attrs, $g_props->attr) ? $this->get_attribute($attrs, $g_props->attr) : '';
             if (is_string($g) && ($g = trim($g)) && !empty($g)) {
                 $group[] = $g;
             } elseif (!empty($g_props->base)) {
                 $group[] = $g_props->base;
             }
             $group = $this->util->apply_filters('get_group_id', $group);
             //Default group
             if (empty($group) || !is_array($group)) {
                 $group = $this->get_prefix();
             } else {
                 $group = implode('_', $group);
             }
             //Set group attribute
             $this->set_attribute($attrs, $g_props->attr, $group);
             unset($g);
         }
         //Activate link
         $this->set_attribute($attrs, 'active');
         //Process internal links
         if ($internal) {
             //Mark as internal
             $this->set_attribute($attrs, 'internal', $pid);
         }
         //Cache item attributes
         $this->cache_media_item($uri, $type, $internal, $props_extra);
         //Filter attributes
         $attrs = $this->util->apply_filters('process_link_attributes', $attrs);
         //Update link in content
         $link_new = '<a ' . $this->util->build_attribute_string($attrs) . '>';
         $content = str_replace($link, $link_new, $content);
     }
     //Handle widget content
     if (!!$this->widget_processing && 'the_content' == current_filter()) {
         $content = $this->exclude_wrap($content);
     }
     return $content;
 }
 /**
  * Initialize default handlers
  * @param SLB_Content_Handlers $handlers Handlers controller
  */
 public function init_defaults($handlers)
 {
     $src_base = $this->util->get_file_url('content-handlers', true);
     $js_path = 'js/';
     $js_path .= SLB_DEV ? 'dev' : 'prod';
     $defaults = array('image' => array('match' => $this->m('match_image'), 'scripts' => array(array('base', "{$src_base}/image/{$js_path}/handler.image.js"))));
     foreach ($defaults as $id => $props) {
         $handlers->add($id, $props);
     }
 }
Beispiel #3
0
 /**
  * Process links in content
  * @global obj $wpdb DB instance
  * @global obj $post Current post
  * @param string $content Text containing links
  * @param string (optional) $group Group to add links to (Default: none)
  * @return string Content with processed links 
  */
 protected function process_links($content, $group = null)
 {
     // Extract links
     $links = $this->get_links($content, true);
     // Do not process content without links
     if (empty($links)) {
         return $content;
     }
     // Process links
     static $protocol = array('http://', 'https://');
     static $qv_att = 'attachment_id';
     static $uri_origin = null;
     if (!is_array($uri_origin)) {
         $uri_parts = array_fill_keys(array('scheme', 'host', 'path'), '');
         $uri_origin = wp_parse_args(parse_url(strtolower(home_url())), $uri_parts);
     }
     static $uri_proto = null;
     if (empty($uri_proto)) {
         $uri_proto = (object) array('raw' => '', 'source' => '', 'parts' => '');
     }
     $uri_parts_required = array('host' => '');
     // Setup group properties
     $g_props = (object) array('enabled' => $this->options->get_bool('group_links'), 'attr' => 'group', 'base' => '', 'legacy_prefix' => 'lightbox[', 'legacy_suffix' => ']');
     if ($g_props->enabled) {
         $g_props->base = is_scalar($group) ? trim(strval($group)) : '';
     }
     // Initialize content handlers
     if (!$this->handlers instanceof SLB_Content_Handlers) {
         $this->handlers = new SLB_Content_Handlers($this);
     }
     // Iterate through and activate supported links
     foreach ($links as $link) {
         // Init vars
         $pid = 0;
         $link_new = $link;
         $uri = clone $uri_proto;
         $type = false;
         $props_extra = array();
         $key = null;
         $internal = false;
         // Parse link attributes
         $attrs = $this->util->parse_attribute_string($link_new, array('href' => ''));
         // Get URI
         $uri->raw = $attrs['href'];
         // Stop processing invalid links
         if (!$this->validate_uri($uri->raw) || $this->has_attribute($attrs, 'active', false)) {
             continue;
         }
         // Normalize URI (make absolute)
         $uri->source = WP_HTTP::make_absolute_url($uri->raw, $uri_origin['scheme'] . '://' . $uri_origin['host']);
         // URI cached?
         $key = $this->get_media_item_id($uri->source);
         // Internal URI? (e.g. attachments)
         if (!$key) {
             $uri->parts = array_merge($uri_parts_required, (array) parse_url($uri->source));
             $internal = $uri->parts['host'] === $uri_origin['host'] ? true : false;
             // Attachment?
             if ($internal && is_local_attachment($uri->source)) {
                 $pid = url_to_postid($uri->source);
                 $src = wp_get_attachment_url($pid);
                 if (!!$src) {
                     $uri->source = $src;
                     $props_extra['id'] = $pid;
                     // Check cache for attachment source URI
                     $key = $this->get_media_item_id($uri->source);
                 }
                 unset($src);
             }
         }
         // Determine content type
         if (!$key) {
             // Get handler match
             $hdl_result = $this->handlers->match($uri->source);
             if (!!$hdl_result->handler) {
                 $type = $hdl_result->handler->get_id();
                 $props_extra = $hdl_result->props;
                 // Updated source URI
                 if (isset($props_extra['uri'])) {
                     $uri->source = $props_extra['uri'];
                     unset($props_extra['uri']);
                 }
             }
             // Cache valid item
             if (!!$type) {
                 $key = $this->cache_media_item($uri, $type, $internal, $props_extra);
             }
         }
         // Stop processing invalid links
         if (!$key) {
             // Cache invalid URI
             $this->validated_uris[$uri->source] = false;
             if ($uri->raw !== $uri->source) {
                 $this->validated_uris[$uri->raw] = false;
             }
             continue;
         }
         // Activate link
         $this->set_attribute($attrs, 'active');
         $this->set_attribute($attrs, 'asset', $key);
         // Mark internal links
         if ($internal) {
             $this->set_attribute($attrs, 'internal', $pid);
         }
         // Set group (if enabled)
         if ($g_props->enabled) {
             $group = array();
             // Get preset group attribute
             $g = $this->has_attribute($attrs, $g_props->attr) ? $this->get_attribute($attrs, $g_props->attr) : '';
             if (is_string($g) && ($g = trim($g)) && !empty($g)) {
                 $group[] = $g;
             } elseif (!empty($g_props->base)) {
                 $group[] = $g_props->base;
             }
             $group = $this->util->apply_filters('get_group_id', $group);
             // Default group
             if (empty($group) || !is_array($group)) {
                 $group = $this->get_prefix();
             } else {
                 $group = implode('_', $group);
             }
             // Set group attribute
             $this->set_attribute($attrs, $g_props->attr, $group);
             unset($g);
         }
         // Filter attributes
         $attrs = $this->util->apply_filters('process_link_attributes', $attrs);
         // Update link in content
         $link_new = '<a ' . $this->util->build_attribute_string($attrs) . '>';
         $content = str_replace($link, $link_new, $content);
     }
     // Handle widget content
     if (!!$this->widget_processing && 'the_content' == current_filter()) {
         $content = $this->exclude_wrap($content);
     }
     return $content;
 }