版权提示插件Copyright-for-Typecho的自定义修改

本站已启用Yves-X/Copyright-for-Typecho的版权插件

修改的初衷是因为我把黑马程序员的Python课件上传了,里面的文章我单独做了版权声明,想着需要简单的设置就能关闭插件的提示。

原版插件在使用的过程中发现了一个问题:switch字段开关在全局打开显示版权信息后,无法通过设置为0,快速关闭版权信息,通过对代码的修改实现了自己的想要的功能,并且将版权提示的样式改成了handsome主题提供的增强样式,版权提示效果图如下:
Snip20181105_2.png
Copyright-for-Typecho插件启用后有全局开关,菜单如下:
Snip20181105_3.png
按照插件文档说明,当全局打开提示时,如果设置switch字段为0,应该是能关闭版权提示的,但是实际使用中无法关闭版权提示信息,而是需要把authorurlnotice三个字段均设为0才会关闭。

字段类型说明示例
switch整数版权信息的开关1
author字符版权作者姓名Yves X
url字符原(本)文链接*https://github.com/Yves-X/Copyright-for-Typecho
notice字符版权声明转载时须注明出处及本声明

字段控制版权信息的代码段:

    private static function localCopyright($widget) {
        $cr = array('switch_on' => '', 'author' => '', 'url' => '', 'notice' => '');
        if ($widget->fields->switch) {
            $cr['switch_on'] = 1;
        }
        $cr['author'] = $widget->fields->author;
        $cr['url'] = $widget->fields->url;
        $cr['notice'] = $widget->fields->notice;
        return $cr;
    }
    private static function apply($widget) {
        $gcr = self::globalCopyright($widget);
        $lcr = self::localCopyright($widget);
        $cr = array('is_enable' => '', 'is_original' => '', 'author' => '', 'url' => '', 'notice' => '');
        if ($widget->is('single')) {
            $cr['is_enable'] = 1;
        }
        if ($widget->parameter->type == 'post' && $gcr['show_on_post'] == 0) {
            $cr['is_enable'] = 0;
        }
        if ($widget->parameter->type == 'page' && $gcr['show_on_page'] == 0) {
            $cr['is_enable'] = 0;
        }
        if ($lcr['switch_on'] != '') {
            $cr['is_enable'] = $lcr['switch_on'];
        }
        if ($gcr['show_url'] == 0) {
            $cr['url'] = 0;
        }
        $cr['url'] = $lcr['url'] != '' ? $lcr['url'] : $gcr['url'];
        if ($gcr['show_url'] == 1 && $lcr['url'] == '') {
            $cr['is_original'] = 1;
            $cr['url'] = $widget->permalink;
        }
        $cr['author'] = $lcr['author'] != '' ? $lcr['author'] : $gcr['author'];
        $cr['notice'] = $lcr['notice'] != '' ? $lcr['notice'] : $gcr['notice'];
        return $cr;
    }

通过查看源代码,发现localCopyright($widget)函数switch字段判断代码段如下:

if ($widget->fields->switch) {
    $cr['switch_on'] = 1;
}

apply($widget)函数最后进行是否开启的判断,通过对$lcr['switch_on']的值做判断是否要开启版权提示信息

if ($lcr['switch_on'] != '') {
    $cr['is_enable'] = $lcr['switch_on'];
}

localCopyright($widget)函数中对switch字段的值进行判断,条件$widget->fields->switch会影响$lcr['switch_on']的值,如果switch空(没有设置)或者为0时,if ($widget->fields->switch)条件为False不会进入代码块,$lcr['switch_on'则为if ($lcr['switch_on'] != '')这个条件也会为False,所以如果全局打开了,即使switch设置为0也不会关闭版权提示信息。
我主要对代码做了如下修改:把switch字段的值取出来,然后在返回之前判断switch的值是不是为0,如果为0,把authorurlnotice三个字段均设为0,这样就可以关闭版权提示了,可以无视全局设置了。

#修改代码
$cr = array('switch_on' => '', 'author' => '', 'url' => '', 'notice' => '', 'switchvar' => '');   

添加的代码段:

#添加的代码段
if ($cr['switchvar'] == 0 ){
$cr['author'] = $widget->fields->switch;
$cr['url'] = $widget->fields->switch;
$cr['notice'] = $widget->fields->switch;
}    

完整代码段:

        private static function localCopyright($widget) {
        $cr = array('switch_on' => '', 'author' => '', 'url' => '', 'notice' => '', 'switchvar' => '');   
        $cr['switchvar'] = $widget->fields->switch;
        if ($widget->fields->switch) {
            $cr['switch_on'] = 1;
        }    
        $cr['author'] = $widget->fields->author;
        $cr['url'] = $widget->fields->url;
        $cr['notice'] = $widget->fields->notice;
        if ($cr['switchvar'] == 0 ){
        $cr['author'] = $widget->fields->switch;
        $cr['url'] = $widget->fields->switch;
        $cr['notice'] = $widget->fields->switch;
        }    
        return $cr;
}

对提示样式的修改,主要对render($cr)函数进行修改。
原代码:

    private static function render($cr) {
        $copyright_html = '';
        $t_author = '';
        $t_notice = '';
        $t_url = '';
        if ($cr['is_enable']) {
            if ($cr['author']) {
                $t_author = '<p class="content-copyright">版权属于:' . $cr['author'] . '</p>';
            }
            if ($cr['url']) {
                if ($cr['is_original']) {
                    $t_url = '<p class="content-copyright">本文链接:<a class="content-copyright" href="' . $cr['url'] . '">' . $cr['url'] . '</a></p>';
                } else {
                    $t_url = '<p class="content-copyright">原文链接:<a class="content-copyright" target="_blank" href="' . $cr['url'] . '">' . $cr['url'] . '</a></p>';
                }
            }
            if ($cr['notice']) {
                $t_notice = '<p class="content-copyright">' . $cr['notice'] . '</p>';
            }
            $copyright_html = '<hr class="content-copyright" style="margin-top:50px" /><blockquote class="content-copyright" style="font-style:normal">' . $t_author . $t_url . $t_notice . '</blockquote>';
        }
        return $copyright_html;
}

修改后的代码如下:

    private static function render($cr) {
        $copyright_html = '';
        $t_author = '';
        $t_notice = '';
        $t_url = '';
        if ($cr['is_enable']) {
            if ($cr['author']) {
                $t_author = '<p><div class="tip inlineBlock info"><strong>版权属于:</strong>' . $cr['author'];
                #$t_author = '<p class="content-copyright">版权属于:' . $cr['author'] . '</p>';
            }
            if ($cr['url']) {
                if ($cr['is_original']) {
                $t_url = '<br/><strong>本文链接:</strong><a href="' . $cr['url'] . '">' . $cr['url'] . '</a>'; 
                } else {
                $t_url = '<br/>原文链接:<a target="_blank" href="' . $cr['url'] . '">' . $cr['url'] . '</a>'; 
                }
            }
            if ($cr['notice']) {
                $t_notice = '<br/>' . $cr['notice'] . '</div></p>';
            }
            $copyright_html = '<hr style="margin-top:50px" /><p>' . $t_author . $t_url . $t_notice . '<p><div class="tip inlineBlock success">感谢阅读,如有错误,欢迎指正。如果觉得对您有帮助,欢迎打赏,谢谢~</div></p>';
        }
        return $copyright_html;
}

$copyright_html主要是对HTML代码的拼接,<div class="tip inlineBlock info"><div class="tip inlineBlock success">这段是handsome主题的增强样式,我通过查博客网页原代码复制来的?。我在最后添加了一段代码,只要装了这个插件并开启插件,就会有提示,而且switch不会把它关闭。
添加的代码段:

'<p><div class="tip inlineBlock success">感谢阅读,如有错误,欢迎指正。如果觉得对您有帮助,欢迎打赏,谢谢~</div></p>'

折腾使我快乐?


版权属于:唧唧咕
本文链接:https://althwn.cc/archives/820/
Notice:本文可随意转载修改不受限制,但文章引用的第三方内容为原作者所有,请勿用于商业目的

感谢阅读,如有错误,欢迎指正。如果觉得对您有帮助,欢迎打赏,谢谢~

Last modification:April 29th, 2021 at 09:53 pm

Leave a Comment