secret
Created

  • Download Gist
front.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
<?php
class SEO_Auto_Linker_Front extends SEO_Auto_Linker_Base
{
/**
* A prefix hash for our SEO autolinker replacements.
*
* @since 0.8
*/
private static $hash;
 
/*
* Container for our post autolinker posts
*/
private static $links;
 
/*
* Container for our options
*/
private static $opts;
 
/*
* Container for the current post's permalink
*/
private static $permalink;
 
/*
* Adds actions and filters and such
*
* @since 0.7
*/
public static function init()
{
self::$hash = md5('seo-auto-linker');
add_filter(
'the_content',
array(get_class(), 'content'),
1
);
}
 
/*
* Main event. Filters the conntent to add links
*
* @since 0.7
*/
public static function content($content)
{
global $post;
if(!self::allowed($post))
return $content;
 
self::setup_links($post);
if(!self::$links)
return $content;
 
$header_replacements = array();
$link_replacements = array();
$other_replacements = array();
$shortcode_replacements = array();
$filtered = $content;
 
preg_match_all('/' . get_shortcode_regex() . '/', $filtered, $scodes);
if(!empty($scodes[0]))
{
$shortcode_replacements = self::gen_replacements($scodes[0], 'shortcode');
$filtered = self::replace($shortcode_replacements, $filtered);
}
 
preg_match_all('/<h[1-6][^>]*>.+?<\/h[1-6]>/iu', $filtered, $headers);
if(!empty($headers[0]))
{
$header_replacements = self::gen_replacements($headers[0], 'header');
$filtered = self::replace($header_replacements, $filtered);
}
 
preg_match_all('/<(img|input)(.*?) \/?>/iu', $filtered, $others);
if(!empty($others[0]))
{
$other_replacements = self::gen_replacements($others[0], 'others');
$filtered = self::replace($other_replacements, $filtered);
}
 
foreach(self::$links as $count => $l)
{
if(apply_filters('seoal_should_continue', false, $l, $count))
{
continue;
}
 
preg_match_all(
'/<a(.*?)href="(.*?)"(.*?)>(.*?)<\/a>/iu',
$filtered,
$links
);
if(!empty($links[0]))
{
$start = count($link_replacements);
$tmp = self::gen_replacements($links[0], 'links', $start);
$filtered = self::replace($tmp, $filtered);
$link_replacements = array_merge(
$link_replacements,
$tmp
);
}
 
$regex = self::get_kw_regex($l);
$url = self::get_link_url($l);
$max = self::get_link_max($l);
if(!$regex || !$url || !$max)
continue;
$target = self::get_link_target($l);
$filtered = preg_replace(
$regex,
'$1<a href="' . esc_url( $url ) . '" title="$2" target="' . $target . '">$2</a>$3',
$filtered,
absint($max)
);
}
 
$filtered = apply_filters('seoal_pre_replace', $filtered, $post);
 
$filtered = self::replace_bak($shortcode_replacements, $filtered);
$filtered = self::replace_bak($header_replacements, $filtered);
$filtered = self::replace_bak($link_replacements, $filtered);
$filtered = self::replace_bak($other_replacements, $filtered);
 
return apply_filters('seoal_post_replace', $filtered, $post);
}
 
/*
* Determins whether or not a post can be editted
*/
protected static function allowed($post)
{
$rv = true;
 
if(
(!is_singular() && apply_filters('seoal_only_single', true)) ||
!in_the_loop()
) $rv = false;
 
if(strpos($post->post_content, '<!--nolinks-->') !== false)
$rv = false;
 
self::$opts = get_option(self::SETTING, array());
if(!isset(self::$opts['blacklist']))
self::$opts['blacklist'] = array();
 
self::$permalink = get_permalink($post);
 
if(in_array(self::$permalink, self::$opts['blacklist']))
$rv = false;
 
return apply_filters('seoal_allowed', $rv, $post);
}
 
/*
* Fetch all of the links posts
*
* @since 0.7
*/
protected static function setup_links($post)
{
 
$links = apply_filters('pre_seoal_links', false, $post);
if(false !== $links)
{
self::$links = $links;
return;
}
 
$links = get_posts(array(
'post_type' => self::POST_TYPE,
'numberposts' => apply_filters('seoal_number_links', -1),
'meta_query' => array(
'relation' => 'AND',
array(
'key' => self::get_key("type_{$post->post_type}"),
'value' => 'on',
'compare' => '='
),
array(
'key' => self::get_key('url'),
'compare' => 'EXISTS' // doesn't do anything, just a reminder
),
array(
'key' => self::get_key('keywords'),
'compare' => 'EXISTS' // doesn't do anything, just a reminder
)
),
'suppress_filters' => false,
));
 
$rv = array();
if($links)
{
foreach($links as $l)
{
$blacklist = self::get_meta($l, 'blacklist');
if(!$blacklist || !in_array(self::$permalink, (array)$blacklist))
$rv[] = $l;
}
}
self::$links = apply_filters('seoal_links', $rv, $post);
}
 
/*
* Get the regex for a link
*
* @since 0.7
*/
protected static function get_kw_regex($link)
{
$keywords = self::get_keywords($link);
if(!$keywords)
return false;
 
// Don't change these unless you know what you're doing. Really.
$ob = apply_filters('seoal_opening_word_boundary', '((?<!\pL))', $link);
$cb = apply_filters('seoal_closing_word_boundary', '((?!\pL))', $link);
 
return sprintf("/{$ob}(%s){$cb}/ui", implode('|', $keywords));
}
 
/*
* fetch the clean and sanitied keywords
*
* @since 0.7
*/
protected static function get_keywords($link)
{
$keywords = self::get_meta($link, 'keywords');
$kw_arr = explode(',', $keywords);
$kw_arr = apply_filters('seoal_link_keywords', $kw_arr, $link);
$kw_arr = array_map('trim', (array)$kw_arr);
$kw_out = array();
foreach($kw_arr as $kw)
{
// Second argument of `preg_quote`? Does not default to `/`
$kw_out[] = preg_quote($kw, '/');
}
return $kw_out;
}
 
/*
* Get the link URL for a keyword
*
* @since 0.7
*/
protected static function get_link_url($link)
{
$meta = self::get_meta($link, 'url');
return apply_filters('seoal_link_url', $meta, $link);
}
 
/*
* Get the maximum number of time a link can be replaced
*
* @since 0.7
*/
protected static function get_link_max($link)
{
$meta = self::get_meta($link, 'times');
$meta = absint($meta) ? absint($meta) : 1;
return apply_filters('seoal_link_max', $meta, $link);
}
 
/*
* Get the target attribute for a given link
*
* @since 0.7.2
* @return string The escaped target att
*/
protected static function get_link_target($link)
{
$target = self::get_meta($link, 'target');
$target = apply_filters('seoal_link_target', $target, $link);
if(!in_array($target, array_keys(self::get_targets())))
{
$target = '_self';
}
return esc_attr($target);
}
 
/*
* Replace get meta
*
* @since 0.7
*/
protected static function get_meta($post, $key)
{
$res = apply_filters('seoal_pre_get_meta', false, $key, $post);
if($res !== false)
{
return $res;
}
if(isset($post->ID))
{
$res = get_post_meta($post->ID, self::get_key($key), true);
}
else
{
$res = '';
}
return $res;
}
 
/*
* Loop through a an array of matches and create an associative array of
* key value pairs to use for str replacements
*
* @todo Look into just hashing the entire array key with md5 or
* something. Might help avoid conflicts?
*
* @since 0.7
*/
protected function gen_replacements($arr, $key, $start=0)
{
$rv = array();
$h = self::$hash;
foreach($arr as $a)
{
$rv["<!--{$h}-{$key}-{$start}-->"] = $a;
$start++;
}
return $rv;
}
 
/*
* Wrapper around str_replace
*
* @since 0.7
*/
protected static function replace($arr, $content)
{
return str_replace(
array_values($arr),
array_keys($arr),
$content
);
}
 
protected static function replace_bak($arr, $content)
{
return str_replace(
array_keys($arr),
array_values($arr),
$content
);
}
}
 
SEO_Auto_Linker_Front::init();

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.