WPG2 Tag Custom URL

The WordPress to Gallery plugin inserts <wpg2>...</wpg2> tags into the post when WPG2 tags are used for gallery thumbnail image  insertions.  The default hyperlink associated with the thumbnail is a link to the photo of the image.

The G2Image module of WPG2 was modified to allow for entry of custom URL hyperlinks on WPG2 tags.  Custom URLs can be used to link to gallery albums or other external locations.  These URLs apply to WPG2 normal or resized thumbnail images.

The WPG2 tag syntax has optional parameters delimited by ‘|’:

<wpg2>n</wpg2> where ‘n’ is the gallery image number
<wpg2>n|size</wpg2> where ‘size’ is the thumbnail maximum pixel width or height
<wpg2>n|link</wpg2> where ‘link’ is the external URL in the form ‘http://…’
<wpg2>n|size|link</wpg2> where ‘size’ and ‘link’ are specified together


wpg2embed.inc

The ImageBlock processing is updated to process the modified syntax for the new ‘link’ parameter.  A ‘link’ specifier is provided to the image block options.  This is output in the generated html. The complete code for the g2_tagimageblock() function is shown:

/*
*********************************************************************************
Gallery2 ImageBlock Handling
*********************************************************************************
*/

/**
* Include image from Gallery2 from WPG2 Tag
*
* @param string $g2inputid Gallery2 Item path relative to root Gallery2 Data directory
* @param integer $g2itemsize Item Size in pixels.  Defaults to null if not included in GET parameters.
* @return string HTML for img tag
*/
function g2_tagimageblock( $g2inputstr ) {

  // Get WPG2 Option Settings
  $wpg2_option = get_option('wpg2_options');

  global $post;

  if ($wpg2_option['g2_validated'] == "Yes") {

    // Initialize Gallery
    if (!defined('G2INIT')) {
      $ret = g2_login();
      if ($ret) {
        echo '<h2>' . __('Fatal G2 error', 'wpg2') . '</h2>' . __("Here's the error from G2: ", 'wpg2') . $ret->getAsHtml();
        exit;
      }
    }

    // Check for the Item Size | parameter & Clean up Strings..
    $g2itempos = strpos ($g2inputstr, '|');
    $g2linkref = "";
    if ($g2itempos) {
      $g2itemsize = substr ($g2inputstr, $g2itempos+1);
      $g2itemid = substr ($g2inputstr, 0, $g2itempos);

      // Check for the link | parameter
      $g2itempos2 = strpos ($g2itemsize, '|');
      if ($g2itempos2) {
        $g2linkref = substr ($g2itemsize, $g2itempos2+1);
        $g2itemsize = substr ($g2itemsize, 0, $g2itempos2);
      } else {
        if ( !is_numeric($g2itemsize) ) {
          $g2linkref = $g2itemsize ;
          $g2itemsize = $wpg2_option['g2_postimgsize'];
      }
    }
  } else {
    $g2itemsize = $wpg2_option['g2_postimgsize'];
      $g2itemid = $g2inputstr;
    }

    if ( !is_numeric($g2itemid) ) {
      // Make Sure Item Path does not contain a + as it should instead be a space
      $g2itemid = str_replace ("+", " ", $g2itemid);

      // Get the Image ID
      list ($ret, $g2itemid) = GalleryCoreAPI::fetchItemIdByPath($g2itemid);
      if ($ret) {
        $img = '* ' . __('WPG2 CANNOT LOCATE GALLERY2 ITEM BY '.$g2itemid, 'wpg2') . ' *';
        return $img;
      }
    } else {
      list ($ret, $g2item) = GalleryCoreApi::loadEntitiesById($g2itemid);
      if ($ret) {
        $img = '* ' . __('WPG2 CANNOT LOCATE GALLERY2 ITEM ID '.$g2itemid, 'wpg2') . ' *';
        return $img;
      }
    }

    // Build the Image Block
    $blockoptions['blocks'] = 'specificItem';
    $blockoptions['show'] = 'none';
    $blockoptions['itemId'] = $g2itemid;
    if ($g2linkref != "")
      $blockoptions['link'] = $g2linkref;

    // Assign Show Details
    if ( $wpg2_option['g2_tagblockshow'] ) {
      if ( count($wpg2_option['g2_tagblockshow']) > 1 )
        $blockoptions['show'] = $wpg2_option['g2_tagblockshow'][1].'|'.$wpg2_option['g2_tagblockshow'][2] ;
      else
        $blockoptions['show'] = $wpg2_option['g2_tagblockshow'][1];
      } else
        $blockoptions['show'] = 'none';

    // Assign maxSize
    if ($g2itemsize)
      $blockoptions['exactSize'] = $g2itemsize;
    else
      if ( $wpg2_option['g2_tagimgsize'] )
        $blockoptions['exactSize'] = $wpg2_option['g2_tagimgsize'];

    // Assign Item Frame Style
    if ($wpg2_option['g2_tagimageframe'])
      $blockoptions['itemFrame']  = $wpg2_option['g2_tagimageframe'];

    // Assign Album Frame Style
    if ($wpg2_option['g2_tagalbumframe'])
      $blockoptions['albumFrame']  = $wpg2_option['g2_tagalbumframe'];

    if ($wpg2_option['wpg2_enabletagslightbox'])
      list ($ret, $img, $headimg, $g2_isalbum ) = g2_getLightImageBlock($blockoptions,$wpg2_option['g2_lightboximgsize'],'wpg2tag-image');
    else
      list ($ret, $img, $headimg, $g2_isalbum ) = g2_getLightImageBlock($blockoptions,null,'wpg2tag-image');

    if ($ret)
      $img = $ret->getAsHtml().print_r($blockoptions);

    $img = preg_replace("/(s+)?(<.+>)(s+)?/", "$2", $img);
    $img = str_replace("n", "", $img); // strip out CRs

    // Requires raw-html plugin because WP filters mess up quotes
    if ($wpg2_option['wpg2_enabletagslightbox'] && !$g2_isalbum) {
      if ($g2linkref == "") {
        $img = str_replace('><.img', ' rel="lightbox['.$post->ID.']"><.img', $img);
      }
      $img = '<.!--start_raw-->' . $img . '<.!--end_raw-->' ;
    }

  } else
    $img = '* ' . __('WPG2 Plugin Not Validated', 'wpg2') . ' *';

  // Add BTEV Event Message
  if (function_exists('btev_trigger_error')) {
    btev_trigger_error('WPG2 TAG G2 IMAGEBLOCK CALL ID ('.$g2inputstr.')', E_USER_NOTICE, __FILE__);
  }

  return $img;
}


wpg2imageblock.tpl

The template file in the WPG2 plugin is used to display the WPG2 tags.  This file required modification to access the link parameter from the ImageBlock and not from the block.  This template file also includes lightbox specific code. The full template file is shown:

{*
 * Author: WPG2 Team, Gallery2 Development Team
 * Author URI: http://wpg2.galleryembedded.com/
 * Updated: 23/06/2007

 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 *}

{foreach from=$ImageBlockData.blocks item=block}
 <table align="center" cellspacing="0" cellpadding="0"><tr><td>
  {if !empty($ImageBlockData.divClass)}
     <div class="{$ImageBlockData.divClass}">
  {else}
    <div class="one-image">
  {/if}
  {if !empty($block.title)}
    <h3> {g->text text=$block.title} </h3>
  {/if}

  {capture assign="linkHref"}{strip}
  {if empty($ImageBlockData.link)}
    {if empty($block.lightboxid)}
      {g->url arg1="view=core.ShowItem" arg2="itemId=`$block.id`" forceFullUrl=true}
    {else}
      {g->url arg1="view=core.DownloadItem" arg2="itemId=`$block.lightboxid`" forceFullUrl=true}
    {/if}
  {elseif $ImageBlockData.link != 'none'}
    {$ImageBlockData.link}
  {/if}
  {/strip}{/capture}

  {capture assign="linkView"}{strip}
  {if empty($ImageBlockData.link)}
    {g->url arg1="view=core.ShowItem" arg2="itemId=`$block.id`" forceFullUrl=true}
  {elseif $ImageBlockData.link != 'none'}
    {$ImageBlockData.link}
  {/if}
  {/strip}{/capture}

  {capture assign="titleText"}{strip}
  {if empty($block.lightboxid) || !empty($ImageBlockData.link)}
    {$block.item.title|markup} {else} {$block.item.title|markup}<br /><a href='{$linkView}'>View Photo Page</a><br />{/if}
  {/strip}{/capture}

  {capture assign="link"}{if !empty($linkHref)}
    <a href="{$linkHref}" {if !empty($ImageBlockData.linkTarget)} target="{$ImageBlockData.linkTarget}" {/if} title="{$titleText}">
  {/if}{/capture}

  {if $block.item.canContainChildren}
    {assign var=frameType value="albumFrame"}
  {else}
    {assign var=frameType value="itemFrame"}
  {/if}
  {if array_key_exists('maxSize', $ImageBlockData)}
    {assign var=maxSize value=$ImageBlockData.maxSize}
  {/if}
  {assign var=imageItem value=$block.item}
  {if isset($block.forceItem)}{assign var=imageItem value=$block.thumb}{/if}
  {if isset($ImageBlockData.$frameType)}
    {g->container type="imageframe.ImageFrame" frame=$ImageBlockData.$frameType
		  width=$block.thumb.width height=$block.thumb.height maxSize=$maxSize}
      {$link}
	{g->image item=$imageItem image=$block.thumb id="%ID%" class="%CLASS%" maxSize=$maxSize forceFullUrl=true}
      {if !empty($linkHref)} </a> {/if}
    {/g->container}
  {else}
    {$link}
      {g->image item=$imageItem image=$block.thumb class="giThumbnail" maxSize=$maxSize forceFullUrl=true}
    {if !empty($linkHref)} </a> {/if}
  {/if}

  {if isset($ImageBlockData.show.title) && isset($block.item.title)}
    <h4 class="giDescription">
      {$block.item.title|markup}
    </h4>
  {/if}

  {if isset($ImageBlockData.show.date) ||
      isset($ImageBlockData.show.views) ||
      isset($ImageBlockData.show.owner)}
    <p class="giInfo">
      {if isset($ImageBlockData.show.date)}
      <span class="summary">
	{g->text text="Date:"} {g->date timestamp=$block.item.originationTimestamp}
      </span>
      {/if}

      {if isset($ImageBlockData.show.views)}
      <span class="summary">
	{g->text text="Views: %d" arg1=$block.viewCount}
      </span>
      {/if}

      {if isset($ImageBlockData.show.owner)}
      <span class="summary">
	{g->text text="Owner: %s" arg1=$block.owner.fullName|default:$block.owner.userName}
      </span>
      {/if}
    </p>
  {/if}
 </div>
</td></tr></table>
{/foreach}


functions.js

In the WPG2 g2image module the insertItems() function requires modification to output the new ‘link’ specifier in the WPG2 tag. The case statement to process the WPG2 tag in this function is modified as shown:

  case 'wpg2_image':
    if (obj.alignment.value != 'none'){
      htmlCode += '<div class="' + obj.alignment.value + '">';
    }
    if(window.tinyMCE) {
      htmlCode += '<img src="' + thumbnail_src[i]
        + '" alt="' + image_id[i];
      if (obj.wpg2_tag_size.value)
        htmlCode += '|' + obj.wpg2_tag_size.value;
      if (obj.custom_url.value)
        htmlCode += '|' + obj.custom_url.value;
      htmlCode += '" title="' + image_id[i];
      if (obj.wpg2_tag_size.value)
        htmlCode += '|' + obj.wpg2_tag_size.value;
      if (obj.custom_url.value)
        htmlCode += '|' + obj.custom_url.value;
      htmlCode += '" ' + thumbw[i] + thumbh[i]
        + 'class="mceItem" id="mce_plugin_g2image_wpg2" />';
    }
    else {
      htmlCode += '<wpg2>' + image_id[i];
      if (obj.wpg2_tag_size.value)
        htmlCode += '|' + obj.wpg2_tag_size.value;
      if (obj.custom_url.value)
        htmlCode += '|' + obj.custom_url.value;
      htmlCode += '</wpg2>';
    }
    if (obj.alignment.value != 'none'){
      htmlCode += '</div>';
    }
  break;


g2image.php

In the WPG2 g2image module the function g2ic_make_html_controls() in g2image.php requires modification to enable the new ‘link’ entry when selecting a WPG2 tag. The initial part of this function is shown:

/**
 * Create the HTML for the image controls
 *
 * @return string $html The HTML for the image controls
 */
function g2ic_make_html_controls(){
	global $gallery, $g2ic_imginsert_options, $g2ic_options;

	// "How to insert:" radio buttons
	$html = "        <fieldset>n"
	. '            <legend>' . T_('Insertion Options') . '</legend>' . "n"
	. '            <label for="alignment">' . T_('How to Insert Image') . '</label>' . "n"
	. g2ic_make_html_select('imginsert', $g2ic_imginsert_options, 'toggleTextboxes();')
	. '            <br />' . "n"
	. '            <br />' . "n"

	// "Custom URL" textbox
	. '            <div name="custom_url_textbox"';
	if ($g2ic_options['default_action'] == 'thumbnail_custom_url' || $g2ic_options['wpg2_valid']) {
		$html .= ' class="displayed_textbox"';
	}
	else {
		$html .= 'class="hidden_textbox"';
	}
	$html .= '>' . "n"
	. '            <label for="custom_url">' . T_('Custom URL') . '<br /></label>' . "n"
	. '            <input type="text" name="custom_url" size="84" maxlength="150" value="' . $g2ic_options['custom_url'] . '" />' . "n"
	. '            <br />' . "n"
	. '            <br />' . "n"
	. '            </div>' . "n"