Created
August 19, 2020 11:04
-
-
Save vivirenremoto/b698a571dc796900b6b1ad18ed80038c to your computer and use it in GitHub Desktop.
fix plugin - Link Juice Optimizer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Link Juice Optimizer | |
Plugin URI: https://www.fedegomez.es | |
Description: Replace the links with a clickable <span> tag to optimize the link juice. | |
Version: 2.0 | |
Author: Fede Gómez | |
Author URI: https://www.fedegomez.es | |
License: GPL3 | |
*/ | |
add_filter('walker_nav_menu_start_el', 'link_juice_optimizer_menu_item', 10, 4); | |
function link_juice_optimizer_menu_item($item_output, $item, $depth, $args) | |
{ | |
if (in_array('ljoptimizer', $item->classes)) { | |
preg_match('/<a\s+(?:[^"\'>]+|"[^"]*"|\'[^\']*\')*href="([^"]+)"|\'[^\']+\'|[^<>\s]+/i', $item_output, $matches, PREG_OFFSET_CAPTURE); | |
$href = $matches[1][0]; | |
$item_output = str_replace('<a', '<span', $item_output); | |
$item_output = str_replace($href, base64url_encode($href), $item_output); | |
$item_output = str_replace('href', 'data-loc', $item_output); | |
$item_output = str_replace('target', 'data-window', $item_output); | |
$item_output = str_replace('_blank', 'new', $item_output); | |
$item_output = str_replace('</a>', '</span>', $item_output); | |
} | |
return $item_output; | |
} | |
add_filter('the_content', 'link_juice_optimizer'); | |
add_filter('widget_text', 'link_juice_optimizer'); | |
function link_juice_optimizer($content) | |
{ | |
$enlacesOriginales = []; | |
$enlacesReemplazo = []; | |
preg_match_all('~(?:\<a.*ljoptimizer.*\>.*\<\/a\>)~iU', $content, $matches); | |
foreach ($matches[0] as $clave => $valor) { | |
$inicio = strrpos($valor, "<a"); | |
$final = strrpos($valor, "</a>"); | |
$a = substr($valor, $inicio, ($final - $inicio) + 4); | |
$xml = new SimpleXMLElement($a); | |
$enlacesOriginales[] = $a; | |
// fix miquel | |
// con este cambio no se pierden otros class usados, ni etiquetas html dentro de los enlaces como por ejemplo img o span | |
//$enlacesReemplazo[] = '<span class="ljoptimizer" data-loc="' . base64url_encode($xml['href']) . '" data-window="' . $target . '">' . $xml[0] . '</span>'; | |
$href = $xml['href']; | |
$item_output = $a; | |
$item_output = str_replace('<a', '<span', $item_output); | |
$item_output = str_replace($href, base64url_encode($href), $item_output); | |
$item_output = str_replace('href', 'data-loc', $item_output); | |
$item_output = str_replace('target', 'data-window', $item_output); | |
$item_output = str_replace('_blank', 'new', $item_output); | |
$item_output = str_replace('</a>', '</span>', $item_output); | |
$enlacesReemplazo[] = $item_output; | |
} | |
foreach ($enlacesOriginales as $clave => $valor) { | |
$content = str_replace($valor, $enlacesReemplazo[$clave], $content); | |
} | |
return $content; | |
} | |
add_filter('dynamic_sidebar_params', 'capture_sidebar', 10, 1); | |
function capture_sidebar($params) | |
{ | |
if (is_admin()) { | |
return $params; | |
} | |
global $wp_registered_widgets; | |
$current_widget_id = $params[0]['widget_id']; | |
if (substr($current_widget_id, 0, 5) == 'text-' || substr($current_widget_id, 0, 12) == 'custom_html-') { | |
return $params; | |
} | |
$wp_registered_widgets[$current_widget_id]['original_callback'] = $wp_registered_widgets[$current_widget_id]['callback']; | |
$wp_registered_widgets[$current_widget_id]['callback'] = 'capture_widget'; | |
return $params; | |
} | |
function capture_widget() | |
{ | |
global $wp_registered_widgets; | |
$original_callback_params = func_get_args(); | |
$widget_id = $original_callback_params[0]['widget_id']; | |
$original_callback = $wp_registered_widgets[$widget_id]['original_callback']; | |
$wp_registered_widgets[$widget_id]['callback'] = $original_callback; | |
$widget_id_base = $original_callback[0]->id_base; | |
$sidebar_id = $original_callback_params[0]['id']; | |
if (is_callable($original_callback)) { | |
ob_start(); | |
call_user_func_array($original_callback, $original_callback_params); | |
$widget_content = ob_get_clean(); | |
$widget_content = link_juice_optimizer($widget_content); | |
echo $widget_content; | |
} | |
} | |
function base64url_encode($data) | |
{ | |
return rtrim(strtr(base64_encode($data), '+/', '-_'), '='); | |
} | |
add_action('wp_enqueue_scripts', 'enqueue_ljo_helpers'); | |
function enqueue_ljo_helpers() | |
{ | |
wp_enqueue_script('ljoptimizer', plugins_url('ljo_helpers.js', __FILE__), array('jquery'), 1.0, true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment