自定义背景{}

自定义背景类。

方法

姓名描述
自定义背景::__construct构造函数–注册管理标头回调。
自定义背景::admin_load为CSS和JavaScript文件设置排队。
自定义背景::admin_page显示自定义背景页。
自定义背景::ajax_Background_add处理向附件添加自定义背景上下文的Ajax请求。
自定义背景::attachment_fields_to_edit已弃用
自定义背景::filter_upload_tabs已弃用
自定义背景::handle_upload处理背景图像的图像上载。
自定义背景::初始化设置自定义后台管理页面的挂钩。
自定义背景::take_action执行自定义背景修改。
自定义背景::wp_set_Background_image已弃用

来源

类Custom_Background{/***管理标头的回调。**@var可调用*@自3.0.0起*/公共$admin_header_callback;/***标题div的回调。**@var可调用*@自3.0.0起*/公共$admin_image_div_callback;/***用于在更新设置并设置为true时触发成功消息。**@自3.0.0起*@var布尔*/私有$updated;/***构造函数-注册管理标头回调。**@自3.0.0起**@param callable$admin_header_callback可选。管理标头回调。*默认空字符串。*@param callable$admin_image_div_callback可选。自定义图像div输出回调。*默认空字符串。*/公共函数__construct($admin_header_callback='',$admin_image_div_callback=''){$this->admin_header_callback=$admin_header_callback;$this->admin_image_div_callback=$admin_mage_div_callback;add_action('admin_menu',数组($this,'init'));add_action('wp_ajax_custom-background-add',数组($this,'ajax_background_add');//自3.5.0以来未使用。add_action('wp_ajax_set-background-image',数组($this,'wp_set_background_image');}/***设置自定义后台管理页面的挂钩。**@自3.0.0起*/公共函数init(){$page=添加主题页面(_x('背景','自定义背景'),_x('背景','自定义背景'),'编辑主题选项','自定义背景',数组($this,'admin_page'));if(!$page){回报;}add_action(“加载-{$page}”,数组($this,'admin_load');add_action(“load-{$page}”,数组($this,'take_action'),49);add_action(“load-{$page}”,数组($this,'handle_upload'),49);if($this->adminheader_callback){add_action(“adminhead-{$page}”,$this->adminheader_callback,51);}}/***为CSS和JavaScript文件设置排队。**@自3.0.0起*/公共函数admin_load(){获取当前屏幕()->添加帮助标签(阵列(“id”=>“overview”,“标题”=>__(“概述”),“内容”=>“<p>”__(“您可以使用自定义背景自定义站点的外观,而无需接触任何主题代码。您的背景可以是图像或颜色。”)</p> '。“<p>”__('要使用背景图像,只需上传它,或单击&#8220;选择图像&#8221;选择已经上传到媒体库的图像;按钮。您可以显示图像的单个实例,也可以平铺图像以填充屏幕。你可以固定你的背景,这样你的网站内容就会在它上面移动,或者你也可以让它与你的网站一起滚动</p> '。“<p>”__('您还可以通过单击“选择颜色”按钮并键入合法的HTML十六进制值来选择背景颜色,例如,&#8220;#ff0000&#8221;表示红色,或使用颜色选择器选择颜色。')</p> '。“<p>”__(“完成后,不要忘记单击“保存更改”按钮。”)</p> ',));获取当前屏幕()->设置帮助屏幕(“<p><strong>”__('有关详细信息:')。'</strong></p>”。“<p>”__('<a href=“https://codex.wordpress.org/Appearance_Background_Screen网站“>自定义背景文档</a>')。'</p>'。“<p>”__('<a href=“https://wordpress.org/support/forums网站/“>支持论坛”));wp_enqueue_media();wp_enqueue_script('自定义背景');wp_enqueue_style('wp-color-picker');}/***执行自定义背景修改。**@自3.0.0起*/公共函数take_action(){if(空($_POST)){回报;}if(isset($_POST[“重置背景”]){check_admin_referer('custom-background-reset','_wpnonce-custom-backgrown-reset');remove_theme_mod('背景图像');remove_theme_mod('background_image_thumb');$this->updated=true;回报;}if(isset($_POST['remove-background'])){//@todo此处未删除上传的文件。check_admin_referer('custom-background-remove','_wpnonce-custom-backgrown-remove');set_theme_mod('背景图像','');set_theme_mod('background_image_thumb','');$this->updated=true;wp_safe_reredirect($_POST['_wp_http_referer']);回报;}if(isset($_POST['background-preset']){check_admin_referer('自定义背景');if(in_array($_POST['background-preset'],数组('default','fill','firt','repeat','custom'),true)){$preset=$_POST['background-preset'];}其他{$preset=“默认”;}set_theme_mod('background_preset',$preset);}if(isset($_POST[“背景位置”]){check_admin_referer('自定义背景');$position=爆炸('',$_POST['background-position']);if(in_array($position[0],array('left','center','right'),true)){$position_x=$position[0];}其他{$position_x=“左”;}if(in_array($position[1],array('top','center','bottom'),true)){$position_y=$position[1];}其他{$position_y=“顶部”;}set_theme_mod('background_position_x',$position_x);set_theme_mod('background_position_y',$position_y);}if(isset($_POST['background-size']){check_admin_referer('自定义背景');if(in_array($_POST['background-size'],数组('auto','contain','cover'),true)){$size=$_POST['background-size'];}其他{$size=“自动”;}set_theme_mod('background_size',$size);}if(isset($_POST['background-repeat']){check_admin_referer('自定义背景');$repeat=$_POST[“背景-重复”];if('no-repeat'!==$repeat){$repeat=“repeat”;}set_theme_mod('background_repeat',$repeat);}if(isset($_POST[“背景-附件”]){check_admin_referer('自定义背景');$attachment=$_POST['background-attachment'];if('fixed'!==$attachment){$attachment=“滚动”;}set_theme_mod('background_attachment',$attachment);}if(isset($_POST['background-color']){check_admin_referer('自定义背景');$color=preg_replace('/[^0-9a-fA-F]/','',$_POST['background-color']);if(strlen($color)===6 ||strlen{set_theme_mod('背景颜色',$color);}其他{set_theme_mod('背景颜色','');}}$this->updated=true;}/***显示自定义背景页。**@自3.0.0起*/公共函数admin_page(){?><div class=“wrap”id=“custom-background”><h1><?php_e(“自定义背景”);?></h1><?php(电话)if(current_user_can('自定义')){$message=冲刺(/*转换器:%s:自定义程序中背景图像配置的URL*/__('您现在可以在<a href=“%s”>Customizer</a>中管理和实时查看自定义背景。'),admin_url('自定义.php?自动聚焦[control]=背景图像'));wp_admin_notice(管理员通知)($消息,阵列(“type”=>“info”,“additional_classes”=>数组(“hide-if-no-customize”),));}if(!空($this->已更新){$updated_message=冲刺(/*转换器:%s:主页URL*/__('背景更新。<a href=“%s”>访问您的网站</a>查看其外观。'),esc_url(主页url(“/”)));wp_admin_notice(管理员通知)($更新消息,阵列(“id”=>“消息”,“additional_classes”=>数组(“updated”),));}?><h2><?php_e(“背景图像”);?></h2><table class=“form-table”role=“presentation”><t车身><tr><th scope=“row”><?php_e(“预览”);?></第>个<td><?php(电话)if($this->adminimagediv_callback){call_user_func($this->admin_image_div_callback);}其他{$background_styles=“”;$bgcolor=获取背景颜色();if($bgcolor){$background_styles.='背景颜色:#$bgcolor.“;”;}$background_image_thumb=获取背景图像();if($background_image_thumb){$background_image_thumb=esc_url(set_url_scheme(get_theme_mod('background_image_thumb',str_replace('%','%%',$background_image_thumb)));$background_position_x=get_theme_mod('背景位置_x',get_themesupport('自定义背景','默认位置-x'));$background_position_y=get_theme_mod('背景位置_y',get_themesupport('自定义背景','默认位置-y'));$background_size=get_theme_mod('background _size',get_themesupport('custom-background','default-size'));$background_repeat=get_theme_mod('background _repeat',get_themesupport('custom-background','default-repeat'));$background_attachment=get_theme_mod('background _attachent',get_themesupport('custom-background','default-attachment'));//背景图像URL必须是单引号,请参见下文。$background_styles.=背景样式“背景图像:url('$background_image_thumb');”.“背景大小:$background_size;”.“背景位置:$background_position_x$background_position_y;”.“背景重复:$background_repeat;”.“背景附件:$background_attachment;”;}?><div id=“custom-background-image”style=“<?php echo$background_styles;?>”><?php//必须是双引号,请参见上文。?><?php if($background_image_thumb){?><img class=“custom-background-image”src=“<?php echo$background_image_thumb;?>”style=“visibility:hidden;”alt=“”/><br/><img class=“custom-background-image”src=“<?php-echo$background_image_thumb;?>”style=“visibility:hidden;”alt=“”/><?php}?></div><?php}?></td></tr><?php if(get_background_image()):?><tr><th scope=“row”><?php_e(“删除图像”);?></第>个<td><form method=“post”><?php wp_nonce_field('custom-background-remove','_wpnonce-custom-backgrown-remove');?><?php submit_button(__(“删除背景图像”),“”,“删除背景”,false);?><br/><?php_e('这将删除背景图像。您将无法恢复任何自定义设置。');?></form></td></tr><?php-endif;?><?php$default_image=get_theme_support(“自定义背景”,“默认图像”);?><?php if($default_image&&get_background_image()!==$默认图像):?><tr><th scope=“row”><?php_e(“还原原始图像”);?></第>个<td><form method=“post”><?php wp_nonce_field(“自定义背景重置”,“wpnonce-custom-background-reset”);?><?php submit_button(__(“还原原始图像”),“”,“重置背景”,false);?><br/><?php_e('这将恢复原始背景图像。您将无法恢复任何自定义设置。');?></form></td></tr><?php-endif;?><?php if(current_user_can(“上传文件”):?><tr><th scope=“row”><?php _e(“选择图像”);?></第>个<td><form enctype=“multipart/form-data”id=“upload-form”class=“wp-upload-fform”method=“post”><p><label for=“upload”><?php_e('从计算机中选择图像:');?></标签><input type=“file”id=“upload”name=“import”/><input type=“hidden”name=“action”value=“save”/><?php wp_nonce_field('自定义备份上传','自定义备份下载');?><?php submit_button(__(“上传”),“”,“提交”,false);?></p><p><label for=“choose-from-library-link”><?php_e('或从媒体库中选择图像:');?></标签><button id=“choose-from-library-link”class=“button”data-choose=“<?php esc_attr_e('选择背景图像');?>”data update=“<?php esc_attr_e('设置为背景');?>”><?php_e(“选择图像”);?></按钮></p></form></td></tr><?php-endif;?></tbody></表格><h2><?php_e(“显示选项”);?></h2><form method=“post”><table class=“form-table”role=“presentation”><t车身><?php if(get_background_image()):?><input name=“background-preset”type=“hidden”value=“custom”><?php(电话)$background_position=冲刺(“%s%s”,get_theme_mod('background_position_x',get_themesupport('custom-background','default-position-x')),get_theme_mod('background_position_y',get_themesupport('custom-background','default-position-y')));$background_position_options=数组(阵列(“左上”=>数组('标签'=>__('左上'),“icon”=>“dashicons dashicons-arrow-left-alt”,),“中心顶部”=>阵列(“标签”=>__(“顶部”),“icon”=>“dashicons dashicons-arrow-up-alt”,),“右上”=>数组('标签'=>__('右上'),“icon”=>“dashicons dashicons-arrow-right-alt”,),),阵列(“左中”=>数组('标签'=>__('左'),“icon”=>“dashicons dashicons-arrow-left-alt”,),“中心”=>数组(“标签”=>__(“中心”),“icon”=>“background-position-center-icon”,),“右中”=>数组(‘标签’=>__(‘右’),“icon”=>“dashicons dashicons-arrow-right-alt”,),),阵列(“左下”=>数组('标签'=>__('左下'),“icon”=>“dashicons dashicons-arrow-left-alt”,),“center bottom”=>数组(“标签”=>__(“底部”),“icon”=>“dashicons dashicons-arrow-down-alt”,),“右下”=>数组(“标签”=>__(“右下角”),“icon”=>“dashicons dashicons-arrow-right-alt”,),),);?><tr><th scope=“row”><?php_e(“图像位置”);?></第>个<td><fieldset><legend class=“screen-reader-text”><span><?php(电话)/*翻译人员:隐藏的辅助功能文本*/_e(“图像位置”);?></span></legment><div class=“background-position-control”><?php foreach($background_position_options作为$group):?><div class=“button-group”><?php-foreach($groupas$value=>$input):?><标签><input class=“ui-helper-hidden-accessible”name=“background-position”type=“radio”value=“<?php-echo esc_attr($value);?>”?php已检查($value,$background_position);?>><span class=“button display-options position”><span class=“<?php echo esc_attr($input['icon']);?>“aria-hidden=”true“></span></span><span class=“屏幕阅读器文本”><?php echo$input[标签];?></span></label><?php endforeach;?></div><?php endforeach;?></div></fieldset></td></tr><tr><th scope=“row”><label for=“background-size”><?php_e(“图像大小”);?></标签><td><fieldset><legend class=“screen-reader-text”><span><?php(电话)/*翻译人员:隐藏的辅助功能文本*/_e(“图像大小”);?></span></legment><select id=“background-size”name=“backgound-size”><选项值=“自动”<?php选中('auto',get_theme_mod('background_size',get-theme_support('custom-background','default-size'));?>><?php_ex(“原始”、“原始大小”);?></选项><option value=“contain”<?php选中(“contain”,get_theme_mod(“background_size”,get-theme_support(“custom-background”,“default-size”));?>><?php_e(“适合屏幕”);?></选项><选项值=“cover”<?php选中(“cover”,get_theme_mod(“background_size”,get-theme_support(“custom-background”,“default-size”));?>><?php_e(“填充屏幕”);?></选项></选择></fieldset></td></tr><tr><th scope=“row”><?php_ex('重复','背景重复');?></第>个<td><fieldset><legend class=“screen-reader-text”><span><?php(电话)/*翻译人员:隐藏的辅助功能文本*/_例如(“重复”,“背景重复”);?></span></legment><input name=“background-repeat”type=“hidden”value=“no-repeap”><label><input type=“checkbox”name=“background-repeat”value=“repeat”<?php已检查(“repeat”,get_theme_mod(“background_repeat“,get_therme_support(“custom-background”,“default-repeat”));?>><?php_e('重复背景图像');?></标签></fieldset></td></tr><tr><th scope=“row”><?php_ex(“滚动”,“背景滚动”);?></第>个<td><fieldset><legend class=“screen-reader-text”><span><?php(电话)/*翻译人员:隐藏的辅助功能文本*/_ex(“滚动”,“背景滚动”);?></span></legment><input name=“background-attachment”type=“hidden”value=“fixed”><label><input name=“background-attachment”type=“checkbox”value=“scroll”<?php已选中(“滚动”,get_theme_mod(“background_attachment”,get-theme_support(“自定义背景”,“默认附件”));?>><?php_e('滚动页面');?></标签></fieldset></td></tr><?php-endif;//获取背景图像()?><tr><th scope=“row”><?php_e(“背景色”);?></第>个<td><fieldset><legend class=“screen-reader-text”><span><?php(电话)/*翻译人员:隐藏的辅助功能文本*/_e(“背景色”);?></span></legment><?php(电话)$default_color=“”;if(current_theme_supports('custom-background','default-color')){$default_color='数据默认颜色=“#”.esc_attr(get_theme_support('自定义背景','默认颜色'))。'“';}?><input type=“text”name=“background-color”id=“backgound-color”value=“#<?php echo esc_attr(get_background _color());?>”?php echo$default_color;?>></fieldset></td></tr></tbody></表格><?php wp_nonce_field(“自定义背景”);?><?php submit_button(null,'primary','save-background-options');?></form></div><?php(电话)}/***处理背景图像的图像上载。**@自3.0.0起*/公共函数handle_upload(){if(空($_FILES)){回报;}check_admin_referer('custom-background-upload','_wpnonce-custom-backgrround-upload');$overrides=数组('test_form'=>false);$uploaded_file=$_FILES[“导入”];$wp_filetype=wp_check_filetype_and_ext($uploaded_file['tmp_name'],$uploaded _file['名称']);if(!wp_match_mime_types('image',$wp_filetype['type'])){wp_die(__('上传的文件不是有效的图像。请重试。');}$file=wp_handle_upload($uploaded_file,$overrides);if(isset($file['error'])){wp_die($file['error']);}$url=$文件[“url”];$type=$file['type'];$file=$file['file'];$filename=wp_basename($file);//构造附件数组。$attachment=数组(“post_title”=>$文件名,'post_content'=>$url,“post_mime_type”=>$type,“guid”=>$url,“context”=>“custom-background”,);//保存数据。$id=wp_insert_attachment($attachment,$file);//添加元数据。wp_update_attachment_metadata($id,wp_generate_attachment_元数据($id、$file));update_post_meta($id,'_wp_attachment_is_custom_background',get_option('stylesheet'));set_theme_mod('背景图像',清理url($url));$thumbia=wp_get_attachment_image_src($id,“缩略图”);set_theme_mod('background_image_thumb',清理url($thumbul[0]));/**该过滤器记录在wp-admin/includes/class-custom-image-header.php中*/$file=apply_filters('wp_create_file_in_uploads',$file,$id);//用于复制。$this->updated=true;}/***处理向附件添加自定义背景上下文的Ajax请求。**当用户从*媒体管理器。**@自4.1.0起*/公共函数ajax_background_add(){check_ajax_referer('background-add','nonce');if(!current_user_can('edit_theme_options')){wp_send_json_error();}$attachment_id=绝对值($_POST['attachment _id']);如果($attachment_id<1){wp_send_json_error();}更新post_meta($attachment_id,'_wp_attachment_is_custom_background',get_stylesheet());wp_send_json_success();}/***@自3.4.0起*@不推荐的3.5.0**@param数组$form_fields*@return数组$form_fields*/公共函数attachment_fields_to_edit($form_fields){返回$form_fields;}/***@自3.4.0起*@不推荐的3.5.0**@param数组$tabs*@return数组$tab*/公共函数filter_upload_tabs($tabs){返回$tabs;}/***@自3.4.0起*@不推荐的3.5.0*/公共函数wp_set_background_image(){check_ajax_referer('自定义背景');if(!current_user_can('edit_theme_options')||!isset($_POST['attachment_id']){出口;}$attachment_id=绝对值($_POST['attachment _id']);$sizes=数组键(/**该过滤器记录在wp-admin/includes/media.php中*/应用筛选器('图像大小名称选择',阵列(‘缩略图’=>__(‘缩略图'),“中等”=>__(“中等”),“大”=>__(“大”),“完整”=>__(“完整尺寸”),)));$size=“缩略图”;if(in_array($_POST['size'],$sizes,true){$size=esc_attr($_POST['size']);}update_post_meta($attachment_id,'_wp_attachment_is_custom_background',get_option('样式表'));$url=wp_get_attachment_image_src($attachment_id,$size);$thumbna=wp_get_attachment_image_src($attachment_id,'thumbna');set_theme_mod('背景图像',清理url($url[0]));set_theme_mod('background_image_thumb',清理url($thumbul[0]));出口;}}

变更日志

版本描述
3.0.0介绍。

用户贡献的笔记

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