Skip to content

Commit

Permalink
Allow embedding path components both before and after the categories/…
Browse files Browse the repository at this point in the history
…tags and slug.
  • Loading branch information
francois committed Jun 5, 2010
1 parent 567c705 commit 0b86e51
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 34 deletions.
121 changes: 90 additions & 31 deletions adgear-ad-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ function adgear_ad() {
if ( adgear_is_dynamic_site() ) {
$format = func_get_arg(0);
$path = func_get_arg(1);
echo "<pre><code>";
var_dump($path);
echo "</code></pre>";

// Switch on $format
$embed_code = get_option( 'adgear_site_universal_embed_code' );
Expand Down Expand Up @@ -139,11 +142,14 @@ function adgear_ad_spots() {

function adgear_ad_handler($atts) {
extract(shortcode_atts(array(
"id" => "",
"format" => "",
"path" => "",
"slugify" => "",
"single" => "",
"id" => "",
"format" => "",
"path" => "",
"slugify" => "",
"single" => "",
"path_pre" => "",
"path_middle" => "",
"path_post" => "",
), $atts));

// If this tag should render only on single posts page, and we're not on a single post, abort
Expand All @@ -152,55 +158,66 @@ function adgear_ad_handler($atts) {
// If this tag should render only on listing pages, and we're on a single post, abort
if ($single == 'no' && is_single()) return "";

// echo "<pre><code>";
// var_dump(array(
// "id" => $id,
// "format" => $format,
// "path" => $path,
// "slugify" => $slugify,
// "single" => $single,
// "site_is_dynamic" => adgear_is_dynamic_site(),
// ));
// echo "</code></pre>";
echo "<pre><code>";
var_dump(array(
"id" => $id,
"format" => $format,
"path" => $path,
"path_pre" => $path_pre,
"path_middle" => $path_middle,
"path_post" => $path_post,
"slugify" => $slugify,
"single" => $single,
"site_is_dynamic" => adgear_is_dynamic_site(),
));
echo "</code></pre>";

if ( !adgear_is_dynamic_site() && $id ) {
return adgear_ad( $id );
} else if ( adgear_is_dynamic_site() && $format && $path ) {
$pathname = array();
$pathname = explode( ',', $path_pre);

switch( $path ) {
case "by_categories":
global $post;
$postcats = get_the_category($post->ID);
$cats = array();
if ( $postcats ) {
foreach( $postcats as $cat ) {
$pathname[] = $cat->cat_name;
$cats[] = $cat->cat_name;
}
}
sort( $pathname );
sort( $cats );
$pathname = array_merge( $pathname, $cats );
break;

case "by_tags":
global $post;
$posttags = get_the_tags($post->ID);
$tags = array();
if ( $posttags ) {
foreach( $posttags as $tag ) {
$pathname[] = $tag->name;
$tags[] = $tag->name;
}
}
sort( $pathname );
sort( $tags );
$pathname = array_merge( $pathname, $tags );
break;

default:
$pathname = explode( ',', $path );
$pathname = array_merge( $pathname, explode( ',', $path ) );
break;
}

$pathname = array_merge( $pathname, explode( ',', $path_middle ) );

if ( $slugify == "1" || $slugify == "yes" ) {
$post = get_post( get_the_ID() );
$pathname[] = $post->post_name;
}

$pathname = array_merge( $pathname, explode( ',', $path_post ) );

return adgear_ad( $format, $pathname);
} else if ( adgear_is_dynamic_site() && $format ) {
return adgear_ad( $format, array() );
Expand Down Expand Up @@ -259,6 +276,27 @@ function adgear_path_type_selector_ui($args) {
<?php
}

function adgear_path_pre_ui($args) {
extract($args);
?>
<input class="adgear_path_pre" type="text" name="<?php echo $name; ?>" id="<?php echo $id; ?>" value="<?php echo $value; ?>"/>
<?php
}

function adgear_path_middle_ui($args) {
extract($args);
?>
<input class="adgear_path_middle" type="text" name="<?php echo $name; ?>" id="<?php echo $id; ?>" value="<?php echo $value; ?>"/>
<?php
}

function adgear_path_post_ui($args) {
extract($args);
?>
<input class="adgear_path_post" type="text" name="<?php echo $name; ?>" id="<?php echo $id; ?>" value="<?php echo $value; ?>"/>
<?php
}

function adgear_format_selector_ui($args) {
extract($args);
?>
Expand Down Expand Up @@ -351,7 +389,7 @@ function update($new_instance, $old_instance) {
$instance = $old_instance;

if ( adgear_is_dynamic_site() ) {
$keys = array( 'format_id', 'path_type', 'path', 'slugify', 'single' );
$keys = array( 'format_id', 'path_type', 'path_pre', 'path', 'path_middle', 'slugify', 'path_post', 'single' );
} else {
$keys = array( 'adspot_id', 'single' );
}
Expand All @@ -375,36 +413,57 @@ function form($instance) {

echo '<div class="adgear-meta" style="margin:0;padding:0">';
if ( adgear_is_dynamic_site() ) {
$format_id = strip_tags($instance['format_id']);
$path_type = strip_tags($instance['path_type']);
$path = strip_tags($instance['path']);
$slugify = strip_tags($instance['slugify']);
$format_id = strip_tags($instance['format_id']);
$path_type = strip_tags($instance['path_type']);
$path = strip_tags($instance['path']);
$slugify = strip_tags($instance['slugify']);

/* Backwards compatibility: don't show ugly error messages when the keys don't exist */
if ( array_key_exists( 'path_pre', $instance ) ) {
$path_pre = strip_tags($instance['path_pre']);
$path_middle = strip_tags($instance['path_middle']);
$path_post = strip_tags($instance['path_post']);
} else {
$path_pre = $path_middle = $path_post = "";
}
?>
<p>
<label for="<?php echo $this->get_field_id('format_id'); ?>">Format:</label>
<label for="<?php echo $this->get_field_id('format_id'); ?>"><?php _e('Ad Format:'); ?></label>
<?php adgear_format_selector_ui( array( 'id' => $this->get_field_id('format_id'), 'name' => $this->get_field_name('format_id'), 'selected' => $format_id, 'include_blank' => true )); ?>
</p>
<p>
<label for="<?php echo $this->get_field_id('path_type'); ?>">Path type:</label>
<label for="<?php echo $this->get_field_id('path_pre'); ?>"><?php _e('Path before:'); ?></label>
<?php adgear_path_pre_ui( array( 'id' => $this->get_field_id('path_pre'), 'name' => $this->get_field_name('path_pre'), 'value' => $path_pre ) ); ?>
</p>
<p>
<label for="<?php echo $this->get_field_id('path_type'); ?>"><?php _e('Path type:'); ?></label>
<?php adgear_path_type_selector_ui( array( 'id' => $this->get_field_id('path_type'), 'name' => $this->get_field_name('path_type'), 'selected' => $path_type, 'path_id' => $this->get_field_id('path'), 'path_name' => $this->get_field_name('path'), 'path_selected' => $path )); ?>
</p>
<p>
<label for="<?php echo $this->get_field_id('slugify'); ?>">Use post's slug in path:</label>
<label for="<?php echo $this->get_field_id('path_middle'); ?>"><?php _e('Path middle:'); ?></label>
<?php adgear_path_middle_ui( array( 'id' => $this->get_field_id('path_middle'), 'name' => $this->get_field_name('path_middle'), 'value' => $path_middle ) ); ?>
</p>
<p>
<label for="<?php echo $this->get_field_id('slugify'); ?>"><?php _e('Use post\'s slug in path:'); ?></label>
<?php adgear_slugify_selector_ui( array( 'id' => $this->get_field_id('slugify'), 'name' => $this->get_field_name('slugify'), 'selected' => $slugify )); ?>
</p>
<p>
<label for="<?php echo $this->get_field_id('path_post'); ?>"><?php _e('Path after:'); ?></label>
<?php adgear_path_post_ui( array( 'id' => $this->get_field_id('path_post'), 'name' => $this->get_field_name('path_post'), 'value' => $path_post ) ); ?>
</p>
<?php
} else {
$adspot_id = strip_tags($instance['adspot_id']);
?>
<p>
<label for="<?php echo $this->get_field_id('adspot_id'); ?>">Ad Spot:</label>
<label for="<?php echo $this->get_field_id('adspot_id'); ?>"><?php _e('Ad spot:'); ?></label>
<?php adgear_adspot_selector_ui( array( 'id' => $this->get_field_id('adspot_id'), 'name' => $this->get_field_name('adspot_id'), 'selected' => $adspot_id )); ?>
</p>
<?php
}
?>
<p>
<label for="<?php $this->get_field_id('single'); ?>">When to show this ad:</label>
<label for="<?php $this->get_field_id('single'); ?>"><?php _e('When to show this ad:'); ?></label>
<?php adgear_single_selector_ui( array( 'id' => $this->get_field_id('single'), 'name' => $this->get_field_name('single'), 'selected' => $single)) ?>
</p>
<div style="display:none;margin:0;padding:0">
Expand Down
21 changes: 19 additions & 2 deletions adgear-ad-manager/adgear-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ function adgearDynamicSiteChange($, root) {
var slugify = root.find(".adgear_slugify_selector").val();
var path = root.find(".adgear_path").val();
var format_id = root.find(".adgear_format_selector").val();
var pathPre = root.find(".adgear_path_pre").val();
var pathMiddle = root.find(".adgear_path_middle").val();
var pathPost = root.find(".adgear_path_post").val();
var pathType = root.find(".adgear_path_type_selector").val();
var single = root.find(".adgear_single_selector").val();
var format = root.find(".adgear_format_selector").get()[0];
Expand Down Expand Up @@ -63,7 +66,21 @@ function adgearDynamicSiteChange($, root) {

if (sendCode) sendCode.css({opacity: 0.5}).get()[0].disabled = true;
} else {
value = "[adgear_ad format=" + format_id + " name=\"" + formatName + "\" single=" + single + " slugify=" + slugify + " path=" + pathParam + "]";
var tagCode = {format : format_id,
name : formatName,
single : single,
slugify : slugify,
path : pathParam,
pre : pathPre,
middle : pathMiddle,
post : pathPost};

value = "[adgear_ad";
for(var key in tagCode) {
value += " " + key + '="' + tagCode[key] + '"';
}
value += "]";

css = {"color": "black", "font-style": "normal"};

if (sendCode) sendCode.css({opacity: 1.0}).get()[0].disabled = false;
Expand All @@ -81,7 +98,7 @@ function adgearDynamicSiteChange($, root) {
adgearStaticSiteChange($, $(ev.target).parents('.adgear-meta'));
});

$(".adgear-meta .adgear_path_type_selector, .adgear-meta .adgear_slugify_selector, .adgear-meta .adgear_format_selector, .adgear-meta .adgear_path, .adgear-meta .adgear_single_selector").live("change", function(ev) {
$(".adgear-meta .adgear_path_type_selector, .adgear-meta .adgear_slugify_selector, .adgear-meta .adgear_format_selector, .adgear-meta .adgear_path, .adgear-meta .adgear_single_selector, .adgear-meta .adgear_path_pre, .adgear-meta .adgear_path_middle, .adgear-meta .adgear_path_post").live("change", function(ev) {
if (!dynamic) return;
adgearDynamicSiteChange($, $(ev.target).parents('.adgear-meta'));
});
Expand Down
20 changes: 19 additions & 1 deletion adgear-ad-manager/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,18 +196,36 @@ function adgear_meta_box_form() {
<?php adgear_format_selector_ui( array( 'id' => 'adgear_format_id', 'name' => 'adgear[format_id]', 'selected' => '', 'include_blank' => true )); ?>
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="adgear_path_pre"><?php _e('Path before:')?></label></th>
<td>
<?php adgear_path_pre_ui( array( 'id' => 'adgear_path_pre', 'name' => 'adgear[path_pre]', 'value' => '' ) ); ?>
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="adgear_type"><?php _e('Path type:')?></label></th>
<td>
<?php adgear_path_type_selector_ui( array( 'id' => 'adgear_type', 'name' => 'adgear[type]', 'selected' => 'categories', 'path_id' => 'adgear_path', 'path_name' => 'adgear[path]', 'path_selected' => '' ) ); ?>
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="adgear_path_middle"><?php _e('Path middle:')?></label></th>
<td>
<?php adgear_path_middle_ui( array( 'id' => 'adgear_path_middle', 'name' => 'adgear[path_middle]', 'value' => '' ) ); ?>
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="adgear_slugify"><?php _e('Use post\'s slug in path:')?></label></th>
<td>
<?php adgear_slugify_selector_ui( array( 'id' => 'adgear_slugify', 'name' => 'adgear[slugify]', 'selected' => 'yes') ); ?>
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="adgear_path_post"><?php _e('Path after:')?></label></th>
<td>
<?php adgear_path_post_ui( array( 'id' => 'adgear_path_post', 'name' => 'adgear[path_post]', 'value' => '' ) ); ?>
</td>
</tr>
<?php } /* dynamic site */ else /* static site */ { ?>
<tr valign="top">
<th scope="row"><label for="adgear_adspot_id"><?php _e('Ad Spot:')?></label></th>
Expand All @@ -225,7 +243,7 @@ function adgear_meta_box_form() {
<tr valign="top">
<th scope="row"><label for="adgear_embed_code"><?php _e('Embed Code:')?></label></th>
<td>
<input type="text" id="adgear_embed_code" name="adgear[embed_code]" size="40" style="width:95%;" autocomplete="off" />
<input type="text" id="adgear_embed_code" name="adgear[embed_code]" size="40" style="width:95%;" autocomplete="off" />
</td>
</tr>
</table>
Expand Down

0 comments on commit 0b86e51

Please sign in to comment.