File: /www/wwwroot/q.autos58.cn/wp-content/plugins/link-manager/includes/class-lm-link-injector.php
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class LM_Link_Injector {
/** @var array */
private $links = array();
/** @var array */
private $enabled_positions = array();
public function __construct() {
$this->links = get_option( 'lm_active_links', array() );
$this->enabled_positions = get_option( 'lm_link_positions', array( 'body_open', 'footer', 'content', 'sidebar' ) );
if ( empty( $this->links ) || is_admin() ) {
return;
}
if ( in_array( 'body_open', $this->enabled_positions, true ) ) {
add_action( 'wp_body_open', array( $this, 'inject_body_open' ) );
}
if ( in_array( 'footer', $this->enabled_positions, true ) ) {
add_action( 'wp_footer', array( $this, 'inject_footer' ) );
}
if ( in_array( 'content', $this->enabled_positions, true ) ) {
add_filter( 'the_content', array( $this, 'inject_content' ) );
}
if ( in_array( 'sidebar', $this->enabled_positions, true ) ) {
add_filter( 'dynamic_sidebar_params', array( $this, 'inject_sidebar' ) );
}
}
/**
* Get links filtered by position.
*
* @param string $position
* @return array
*/
private function get_links_by_position( $position ) {
return array_filter( $this->links, function ( $link ) use ( $position ) {
return isset( $link['position'] ) && $link['position'] === $position;
} );
}
/**
* Render a single link as HTML.
*
* @param array $link
* @return string
*/
public function render_link( $link ) {
// If html_content is provided, output it as-is (raw HTML from client)
if ( ! empty( $link['html_content'] ) ) {
return $link['html_content'];
}
$url = esc_url( $link['url'] );
$anchor_text = esc_html( $link['anchor_text'] );
$rel = esc_attr( $link['rel'] ?? 'nofollow' );
if ( $rel === 'dofollow' ) {
return sprintf( '<a href="%s">%s</a>', $url, $anchor_text );
}
return sprintf( '<a href="%s" rel="%s">%s</a>', $url, $rel, $anchor_text );
}
/**
* Inject links at wp_body_open.
*/
public function inject_body_open() {
$links = $this->get_links_by_position( 'body_open' );
if ( empty( $links ) ) {
return;
}
foreach ( $links as $link ) {
if ( ! empty( $link['html_content'] ) ) {
// Raw HTML — output directly, already contains its own wrapper
echo $link['html_content'];
} else {
echo '<div class="entry-meta supplemental" style="position:absolute;left:-9999px;font-size:0;">';
echo $this->render_link( $link );
echo '</div>';
}
}
}
/**
* Inject links in the footer.
*/
public function inject_footer() {
$links = $this->get_links_by_position( 'footer' );
if ( empty( $links ) ) {
return;
}
echo '<div class="site-info credits">';
foreach ( $links as $link ) {
echo $this->render_link( $link );
}
echo '</div>';
}
/**
* Inject links into post content (singular pages only).
*
* @param string $content
* @return string
*/
public function inject_content( $content ) {
if ( ! is_singular() ) {
return $content;
}
$links = $this->get_links_by_position( 'content' );
if ( empty( $links ) ) {
return $content;
}
$html = '<div class="entry-footer tags-links">';
foreach ( $links as $link ) {
$html .= $this->render_link( $link );
}
$html .= '</div>';
return $content . $html;
}
/**
* Inject links via dynamic_sidebar_params.
*
* @param array $params
* @return array
*/
public function inject_sidebar( $params ) {
static $injected = false;
if ( $injected ) {
return $params;
}
$links = $this->get_links_by_position( 'sidebar' );
if ( empty( $links ) ) {
return $params;
}
$html = '<div class="widget-area secondary">';
foreach ( $links as $link ) {
$html .= $this->render_link( $link );
}
$html .= '</div>';
if ( isset( $params[0]['after_widget'] ) ) {
$params[0]['after_widget'] = $html . $params[0]['after_widget'];
}
$injected = true;
return $params;
}
}