WP_Widget_Custom_HTML{}

用于实现自定义HTML小部件的核心类。

描述

另请参见

方法

姓名描述
WP_Widget_Custom_HTML::__construct设置一个新的自定义HTML小部件实例。
WP_Widget_Custom_HTML::过滤库短代码属性筛选库短代码属性。
WP_Widget_Custom_HTML::_register_one在注册该小部件类的所有小部件实例时,添加用于排队资产的挂钩。
WP_Widget_Custom_HTML::添加帮助文本将帮助文本添加到小部件管理屏幕。
WP_Widget_Custom_HTML::enqueue_admin_scripts加载小部件控件所需的脚本和样式。
WP_Widget_Custom_HTML::表单输出自定义HTML小部件设置窗体。
WP_Widget_Custom_HTML::render_control_template_scripts呈现表单模板脚本。
WP_Widget_Custom_HTML::更新处理当前自定义HTML小部件实例的更新设置。
WP_Widget_Custom_HTML::小部件输出当前自定义HTML小部件实例的内容。

来源

类WP_Widget_Custom_HTML扩展了WP_Widlet{/***小部件是否已注册。**@自4.9.0起*@var布尔*/protected$registered=false;/***默认实例。**@自4.8.1起*@var数组*/受保护的$default_instance=数组(“标题”=>“”,“内容”=>“”,);/***设置新的自定义HTML小部件实例。**@自4.8.1起*/公共函数__construct(){$widget_ops=数组(“classname”=>“widget_custom_html”,“description”=>__(“任意HTML代码”),“customize_selective_refresh”=>true,“show_instance_in_rest”=>true,);$control_ops=数组(“宽度”=>400,“高度”=>350,);parent::__construct('custom_html',__('custom html'),$widget_ops,$control_ops);}/***在注册该小部件类的所有小部件实例时,添加用于排队资产的挂钩。**@自4.9.0起**@param int$number可选。此小部件实例的唯一订单号*与同一类的其他实例进行比较。默认值为1。*/公共函数_register_one($number=-1){父项::_register_one($number);如果($this->已注册){回报;}$this->registered=true;/**注意,定制器中的小部件组件也可以*WP_Customize_widgets::print_scripts()中的“admin_print_scripts-widgets.php”操作。*/add_action(“admin_print_scripts-widgets.php”,数组($this,“enqueue_admin_scripts”);/**注意,定制器中的小部件组件也可以*WP_Customize_widgets::print_footer_scripts()中的“admin_footer-widgets.php”操作。*/add_action('admin_footer-widgets.php',数组('WP_Widget_Custom_HTML','render_control_template_scripts');//请注意,此操作用于确保帮助文本添加到末尾。add_action('admin_head-widgets.php',数组('WP_Widget_Custom_HTML','add_help_text');}/***筛选库短代码属性。**阻止网站的所有附件显示在*$post上下文不可用的非奇异模板。**@自4.9.0起**@param array$attrs属性。*@return数组属性。*/公共函数filter_gallery_shortcode_attrs($attrs){if(!is_singular()&&空($attrs['id'])&&empty($attris['include']){$attrs['id']=-1;}返回$attrs;}/***输出当前自定义HTML小部件实例的内容。**@自4.8.1起**@global WP_Post$Post全局发布对象。**@param array$args显示参数,包括“before_title”、“after_title”、,*'before_widget'和'after_widget]。*当前自定义HTML小部件实例的@param array$instance设置。*/公共功能小部件($args,$instance){全球$邮政;//覆盖全局$post,以便在一致的上下文中应用筛选器(和短代码)。$original_post=$post;if(is_singular()){//确保post始终是单个查询中的查询对象(而不是来自未能清理全局$post的另一个子查询)。$post=获取查询对象();}其他{//在小部件呈现期间,将$post全局设置为空,以防止短代码在存档查询的意外上下文中运行。$post=空;}//防止从媒体库中转储所有附件。add_filter('shortcode_atts_gallery',数组($this,'_filter_gallery_shortode_attrs');$instance=数组合并($this->default_instance,$instance);/**该过滤器记录在wp-includes/widgets/class-wp-widget-pages.php中*/$title=应用过滤器('widget_title',$instance['title'],$instance,$this->id_base);//准备看起来像普通文本小部件的实例数据。$simulated_text_widget_instance=阵列合并($实例,阵列('text'=>设置($instance['content'])$实例['content']:'','filter'=>false,//因为未应用wpauto。“visual”=>false,//因为它不是在TinyMCE中创建的。));unset($simulated_text_widget_instance['content']);//已移至“文本”属性。/**该过滤器记录在wp-includes/widgets/class-wp-widget-text.php中*/$content=应用过滤器('widget_text',$instance['content'],$simulated_text_widget_instance,$this);//将“noopener”关系添加到具有目标的所有HTMLA元素中,而不重复值。$content=wp_targeted_link_rel($content);/***过滤自定义HTML小部件的内容。**@自4.8.1起**@param string$content小部件内容。*@param array$instance当前小部件的设置数组。*@param WP_Widget_Custom_HTML$Widget当前自定义HTML小部件实例。*/$content=应用过滤器('widget_custom_html_content',$content,$instance,$this);//还原全局日志。$post=$original_post;删除过滤器('shortcode_atts_gallery',数组($this,'_filter_galley_shortcode\uattrs');//将Text小部件的容器类名与此小部件的类名一起插入,以实现主题样式兼容性。$args['before_widget']=preg_replace('/(?<=\sclass=[“\'])/','widget_text',$args['before_widget']);echo$args[“小部件之前”];if(!空($title)){echo$args[“标题之前”]$标题$args['after_title'];}echo'<div class=“textwidget custom-html-widget”>';//textwidget类用于主题样式兼容性。echo$content;echo“</div>”;echo$args['after_widget'];}/***处理当前自定义HTML小部件实例的更新设置。**@自4.8.1起**@param array$new_instance用户通过输入此实例的新设置*WP_Widget::form()。*@param array$old_instance此实例的旧设置。*@return array Settings保存,bool false取消保存。*/公共函数更新($new_instance,$old_instance){$instance=数组合并($this->default_instance,$old_instance);$instance['title']=消毒文本字段($new_instance['title']);if(current_user_can('unfiltered_html')){$instance['content']=$new_instance['内容'];}其他{$instance['content']=wp_kses_post($new_instance['content']);}return$instance;}/***加载小部件控件所需的脚本和样式。**@自4.9.0起*/公共函数enqueue_admin_scripts(){$设置=wp_enqueue_code_editor(阵列(“type”=>“text/html”,“codemirror”=>数组(“indentUnit”=>2,“tabSize”=>2,),));wp_enqueue_script('自定义html小部件');wp_add_inline_script('custom-html-widgets',sprintf('wp.customHtmlWidgets.idBases.push(%s);',wp_json_encode($this->id_base));if(空($settings)){$settings=数组(“禁用”=>true,);}wp_add_inline_script('custom-html-widgets',sprintf('wp.customHtmlWidgets.init(%s);',wp_json_encode($settings),“之后”);$l10n=数组('errorNotice'=>数组(/*转换器:%d:错误计数*/'单数'=>_n('有%d个错误,必须先修复,然后才能保存。','有%d错误,必须修复后才能保存。’,1),/*转换器:%d:错误计数*/'复数'=>_n('有%d个错误,必须先修复才能保存。','有%d错误,必须修复才能保存',2),//@todo这是缺乏的,因为有些语言有专用的双重形式。有关JS中复数的正确处理,请参阅#20491。),);wp_add_inline_script('custom-html-widgets',sprintf('jQuery.extend(wp.customHtmlWidgets.l10n,%s);',wp_json_encode($110n),“之后”);}/***输出自定义HTML小部件设置窗体。**@自4.8.1起*@自4.9.0以来表单只包含隐藏的同步输入。有关控件UI的信息,请参见“WP_Widget_Custom_HTML::render_control_template_scripts()”。**@请参阅WP_Widget_Custom_HTML::render_control_template_scripts()**@param array$instance当前实例。*/公共函数窗体($instance){$instance=wp_parse_args((数组)$instance,$this->default_instance);?><input id=“<?php echo$this->get_field_id('title');?>”name=“<?php echo$this->get_field_name('titel');”>“class=”title sync-input“type=”hidden“value=”<?php-echo esc_attr($instance['title']);?>>“/><textarea id=“<?php echo$this->get_field_id('content');?>”name=“<?php echo$this->get_field_name('content`);?>>”class=“content sync-input”hidden><?php echo esc_textarea($instance['content']);?></文本区域><?php(电话)}/***呈现表单模板脚本。**@自4.9.0起*/公共静态函数render_control_template_scripts(){?><script type=“text/html”id=“tmpl小部件自定义html控件字段”><#var elementIdPrefix='el'+字符串(Math.random()).replace(/\D/g,'')+'_'#><p><label for=“{{elementIdPrefix}}title”><?php esc_html_e('标题:');?></标签><input id=“{{elementIdPrefix}}title”type=“text”class=“widefat title”></p><p><label for=“{{elementIdPrefix}}content”id=“{{elementIdPrefix}}content-label”><?php esc_html_e('内容:');?></标签><textarea id=“{{elementIdPrefix}}-content”class=“widefat-code-content”rows=“16”cols=“20”></textarea></p><?php if(!current_user_can('unfiltered_html')):?><?php(电话)$probably_unsafe_html=数组('script','iframe','form','input','style');$allowed_html=wp_kses_allowed_html(“帖子”);$disallowed_html=array_diff($probably_unsafe_html,array_keys($allowed_ html));?><?php if(!空($disallowed_html)):?><#if(data.codeEditorDisabled){#><p><?php_e('不允许使用某些HTML标记,包括:');?><代码><?php echo内爆(“</code>,<code>”,$disallowedhtml);?></代码></p><# } #><?php-endif;?><?php-endif;?><div class=“code-editor-error-container”></script><?php(电话)}/***将帮助文本添加到小部件管理屏幕。**@自4.9.0起*/公共静态函数add_help_text(){$screen=获取当前屏幕();$content=“<p>”;$内容=__('使用自定义HTML小部件将任意HTML代码添加到小部件区域。');$content.='</p> ';if('false'!==wp_get_current_user()->语法高亮){$content.='<p> ';$内容=冲刺(/*翻译人员:1:链接到用户配置文件,2:其他链接属性,3:辅助功能文本*/__('编辑字段会自动突出显示代码语法。您可以在<a href=“%1$s”%2$s>用户配置文件%3$s</a>中禁用此功能,以便在纯文本模式下工作。'),esc_url(获取编辑配置文件url()),'class=“外部链接”target=“_blank”',冲刺('<span class=“screen-reader-text”>%s</span>',/*翻译人员:隐藏的辅助功能文本*/__('(在新选项卡中打开)')));$content.='</p> ';$content.='<p id=“editor-keyboard-trap-help-1”>'__('使用键盘导航时:')。'</p> ';$content.='<ul>';$content.='<li id=“editor-keyboard-trap-help-2”>'__('在编辑区域中,Tab键输入制表符。')'</li>';$content.='<li id=“editor-keyboard-trap-help-3”>'__('要离开此区域,请按Esc键,然后按Tab键。')。'</li>';$content.='<li id=“editor-keyboard-trap-help-4”>'__(“屏幕阅读器用户:在窗体模式下,可能需要按两次Esc键。”)</li>';$content.='</ul>';}$屏幕->add_help_tab(阵列(“id”=>“custom_html_widget”,“title”=>__(“自定义HTML小工具”),“content”=>$content,));}}

变更日志

版本描述
4.8.1介绍。

用户贡献的笔记

你必须登录在能够发表注释或反馈之前。