自定义图像标题{}

自定义标题图像类。

方法

姓名说明
自定义图像标题::__construct构造函数–注册管理标头回调。
自定义图像标题::admin_page根据当前步骤显示页面。
自定义图像标题::ajax_Header_add给定标题图像的附件ID,将其“上次使用”的时间戳更新到现在。
自定义图像标题::ajax_Header_crop获取Media Manager上载的附件,将其裁剪,然后将其保存为新对象。返回JSON编码的对象详细信息。
自定义图像标题::ajax_Header_remove给定标题图像的附件ID,将其取消设置为活动主题的用户加载标题图像。
自定义图像标题::attachment_fields_to_edit自3.5.0以来未使用。
自定义图像标头::create_attachment_object创建附件“对象”-已弃用
自定义图像标题::css_includes设置CSS文件的排队。
自定义图像标题::customize_set_last_used通过Customizer保存新的页眉图像后,更新页眉图像附件上最近使用的邮戳。
自定义图像标题::过滤器上传标签自3.5.0以来未使用。
Custom_Image_Header::已完成显示自定义页眉图像页面的最后一步。
自定义图像标题::get_default_Header_images获取默认标头图像的详细信息(如果已定义)。
自定义图像标题::get_Header_dimensions根据当前所选主题支持的内容计算宽度和高度。
自定义图像标题::get_previous_crop从同一基本图像中获取上一个裁剪的ID。
Custom_Image_Header::获取上传的Header_images获取以前上载的标题图像。
自定义图像标题::help添加上下文帮助。
自定义图像标题::init设置自定义标题管理页面的挂钩。
自定义图像标题::insert_attachment插入附件及其元数据。
自定义图像标题::js根据步骤执行JavaScript。
自定义图像标题::js_1根据步骤1和3显示JavaScript。
自定义图像标题::js_2根据步骤2显示JavaScript。
自定义图像标题::js_includes为JavaScript文件设置排队。
自定义图像标题::process_default_headers处理默认标题。
自定义图像标题::remove_Header_Image删除标题图像。
自定义图像标题::reset_Header_Image将标题图像重置为主题的默认图像。
自定义图像标头::设置标头图像从现有上载的标头和默认标头中选择标头图像,或提供上载标头数据的数组(新数据或媒体库中的数据)。
自定义图像标题::show_Header_selector显示用于选择多个默认标题之一的UI。
Custom_Image_Header::步骤获取当前步骤。
自定义图像标题::step_1显示自定义页眉图像页面的第一步。
自定义图像标题::步骤2显示自定义页眉图像页面的第二步。
自定义图像标题::step_2_manage_upload上载要在第二步中裁剪的文件。
自定义图像标头::步骤3显示自定义页眉图像页面的第三步。
自定义图像标题::take_action执行自定义标头修改。

来源

类Custom_Image_Header{/***管理标头的回调。**@var可调用*@自2.1.0起*/公共$admin_header_callback;/***标题div的回调。**@var可调用*@自3.0.0起*/公共$admin_image_div_callback;/***保留默认标题。**@var数组*@自3.0.0起*/public$default_headers=array();/***用于在更新设置并设置为true时触发成功消息。**@自3.0.0起*@var布尔*/私有$updated;/***构造函数-注册管理标头回调。**@自2.1.0起**@param callable$admin_header_callback Administration头回调。*@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(“customize_save_after”,数组($this,“customise_set_last_used”);add_action('wp_ajax_custom-header-crop',数组($this,'ajax-header_crop');add_action('wp_ajax_custom-header-add',数组($this,'ajax_header_add');add_action('wp_ajax_custom-header-remove',数组($this,'ajax_header_remove');}/***设置自定义标题管理页面的挂钩。**@自2.1.0起*/公共函数init(){$page=添加主题页面(_x('标题','自定义图像标题'),_x('标题','自定义图像标题'),'编辑主题选项','自定义标题',数组($this,'admin_page'));if(!$page){回报;}add_action(“admin_print_scripts-{$page}”,数组($this,'jsincludes');add_action(“admin_print_styles-{$page}”,数组($this,'css_includes'));add_action(“adminhead-{$page}”,数组($this,'help');add_action(“admin_head-{$page}”,数组($this,'take_action'),50);add_action(“adminhead-{$page}”,数组($this,'js'),50);if($this->adminheader_callback){add_action(“adminhead-{$page}”,$this->adminheader_callback,51);}}/***添加上下文帮助。**@自3.0.0起*/公共函数帮助(){获取当前屏幕()->添加帮助标签(阵列(“id”=>“overview”,“标题”=>__(“概述”),“内容”=>“<p>”__('此屏幕用于自定义主题的标题部分。')'</p> '。“<p>”__('您可以从主题的默认标题图像中选择,也可以使用自己的标题图像。您还可以自定义网站标题和标语的显示方式。')<p> ',));获取当前屏幕()->添加帮助标签(阵列(“id”=>“set-header-image”,'标题'=>__('标题图像'),“内容”=>“<p>”__(“您可以为您的网站设置自定义图像标题。只需上传图像并进行裁剪,新标题就会立即生效。或者,您也可以通过单击&#8220;选择图像&#8221;按钮,使用已经上传到媒体库的图像。”)</p> '。“<p>”__('一些主题附带了附加的标题图像。如果您看到显示了多个图像,请选择您想要的图像,然后单击&#8220;保存更改&#8221;按钮。')</p> '。“<p>”__(“如果您的主题有多个默认标题图像,或者您上传了多个自定义标题图像,您可以选择让WordPress在站点的每个页面上随机显示不同的图像。单击“上传的图像”或“默认图像”部分旁边的“随机”单选按钮以启用此功能。”)</p> '。“<p>”__('如果您根本不想在站点上显示页眉图像,请单击此页面页眉图像部分底部的&#8220;删除页眉图像&#8221;按钮。如果您想稍后重新启用页眉图片,只需选择其他图像选项之一,然后单击&#8220,保存更改。')。'</p> ',));获取当前屏幕()->添加帮助标签(阵列(“id”=>“set-header-text”,'标题'=>__('标题文本'),“内容”=>“<p>”。冲刺(/*翻译器:%s:“常规设置”屏幕的URL*/__('对于大多数主题,标题文本是您的网站标题和标语,如<a href=“%s”>常规设置</a>部分中所定义。'),admin_url(“options-general.php”)) .“</p>”。“<p>”__('在此页面的“标题文本”部分中,您可以选择是显示还是隐藏此文本。您还可以通过单击“选择颜色”按钮并键入合法的HTML十六进制值来选择文本的颜色,例如,&#8220;#ff0000&#8221;表示红色,或使用颜色选择器选择颜色。')</p> '。“<p>”__('完成后,请不要忘记单击&#8220;保存更改&#8221;')。'</p> ',));获取当前屏幕()->设置帮助屏幕(“<p><strong>”__('有关详细信息:')。'</strong></p>”。“<p>”__('<a href=“https://codex.wordpress.org/Appearance_Header_Screen网站“>自定义标题上的文档</a>').'</p>'。“<p>”__('<a href=“https://wordpress.org/support/forums网站/“>支持论坛”));}/***获取当前步骤。**@自2.6.0起**@return int当前步骤。*/公共函数step(){if(!isset($_GET['step']){返回1;}$step=(int)$_GET['step'];if($step<1|3<$step||(2===$step&&!wp_verify_nonce($_REQUEST['_wpnonce-custom-header-upload'],'custom-header-rupload'))||(3===$step&&!wp_verify_nonce($_REQUEST['_wpnonce'],'custom-header-crop-image')) {返回1;}return$step;}/***为JavaScript文件设置排队。**@自2.1.0起*/公共函数js_includes(){$step=$this->step();if((1===$step ||3===$step)){wp_enqueue_media();wp_enque_script(“自定义标头”);if(current_theme_supports('自定义标题','标题文本')){wp_enqueue_script('wp-color-picker');}}elseif(2===$step){wp_enqueue_script(“imgareaselect”);}}/***设置CSS文件的排队。**@自2.7.0起*/公共函数css_includes(){$step=$this->step();if((1===$step||3==$step)&&current_theme_supports('custom-header','header-text')){wp_enqueue_style('wp-color-picker');}elseif(2===$step){wp_enqueue_style(“imgareaselect”);}}/***执行自定义标头修改。**@自2.6.0起*/公共函数take_action(){if(!current_user_can('edit_theme_options')){回报;}if(空($_POST)){回报;}$this->updated=true;if(isset($_POST['resetheader'])){check_admin_referer('自定义header-options','_wpnonce-custom-header-options');$this->reset_header_image();回报;}if(isset($_POST['removeheader'])){check_admin_referer('自定义header-options','_wpnonce-custom-header-options');$this->remove_header_image();回报;}if(isset($_POST['text-color'])&&!isset($_POST['display-header-text']){check_admin_referer('自定义header-options','_wpnonce-custom-header-options');set_theme_mod('header_textcolor','blank');}elseif(isset($_POST['text-color'])){check_admin_referer('自定义header-options','_wpnonce-custom-header-options');$_POST['text-color']=str_replace('#','',$_POST['text-color']);$color=preg_replace('/[^0-9a-fA-F]/','',$_POST['text-color']);if(strlen($color)===6 ||strlen{set_theme_mod('header_textcolor',$color);}elseif(!$color){set_theme_mod('header_textcolor','blank');}}if(isset($_POST['default-header'])){check_admin_referer('自定义header-options','_wpnonce-custom-header-options');$this->set_header_image($_POST['default-header']);回报;}}/***处理默认标题。**@自3.0.0起**@global数组$_wp_default_headers*/公共函数process_default_headers(){全局$_wp_default_headers;if(!isset($_wp_default_headers)){回报;}if(!empty($this->default_headers)){回报;}$this->default_headers=$_wp_default_header;$template_directory_uri=获取模板目录uri();$stylesheet_directory_uri=获取样式表目录uri();foreach(array_keys($this->default_headers)作为$header){$this->default_headers[$header]['url']=sprintf($this->default_headers[$header]['url'],$template_directory_uri,$stylesheet_directory_uri);$this->default_headers[$header]['tumbnail_url']=sprintf($this->default_headers[$header]['tumbnail_url'],$template_directory_uri,$stylesheet_directory_uri);}}/***显示用于选择多个默认标题之一的UI。**如果此主题有多个标题图像,则显示随机图像选项。*如果未设置标题,则默认情况下启用“随机图像”选项。**@自3.0.0起**@param string$type头类型。“default”之一(用于Uploaded Images控件)*或“uploaded”(用于uploaded Images控件)。*/公共函数show_header_selector($type='default'){if(“默认”===$type){$headers=$this->default_headers;}其他{$headers=get_uploaded_header_images();$type=“已上传”;}if(1<count($headers)){echo'<div class=“random-header”>';echo'<label><input name=“default-header”type=“radio”value=“random-'.$type.'-image”'。选中(is_random_header_image($type),true,false)。'/>';_e('<strong>随机:</strong>在每页上显示不同的图像。');echo“</label>”;echo“</div>”;}echo'<div class=“available-headers”>';foreach($headers作为$header_key=>$header){$header_thumbnail=$header['tumbnail_url'];$header_url=$header['url'];$header_alt_text=空($header['alt_text'])?'':$标题['alt_text'];echo'<div class=“default header”>';echo'<label><input name=“default header”type=“radio”value=“'.esc_attr($header_key).'”'。选中($header_url,get_theme_mod('header_image'),false)。'/>';$width=“”;if(!空($header['attachment_id']){$width='width=“230”';}echo“<img src=”“.esc_url(set_url_scheme($header_thumbnail)).'”alt=“'.esc_attr($header _alt_text).'“”$宽度。'/></标签>';echo“</div>”;}echo'<div class=“clear”></div></div>';}/***根据步骤执行JavaScript。**@自2.1.0起*/公共函数js(){$step=$this->step();if((1===$step||3==$step)&&current_theme_supports('custom-header','header-text')){$this->js_1();}elseif(2===$step){$this->js_2();}}/***根据步骤1和3显示JavaScript。**@自2.6.0起*/公共函数js_1(){$default_color=“”;if(current_theme_supports('custom-header','default-text-color')){$default_color=get_theme_support('custom-header','default-text-color');if($default_color&&!str_contains($default颜色,'#')){$default_color=“#”$默认颜色;}}?><script type=“text/javascript”>(函数($){var default_color='<?php echo esc_js($default_color);?>',标题_文本_字段;函数pickColor(颜色){$('#name').css('color',color);$('#desc').css('color',color);$('#text-color').val(颜色);}函数toggle_text(){var checked=$('#display-header-text').prop('checked'),文本颜色;header_text_fields.toggle(选中);if(!已选中)回报;text_color=$('#text color');if(“”===text_color.val().replace(“#”,“”)){text_color.val(默认颜色);pickColor(默认颜色);}其他{pickColor(text_color.val());}}$(函数(){var text-color=$('#text-color');header_text_fields=$('.displaying-header-text');text_color.wp颜色选取器({更改:函数(事件、ui){pickColor(text_color.wpColorPicker('color'));},清除:函数(){pickColor(“”);}});$(“#display-header-text”)点击(toggle_text);<?php如果(!display_header_text()):?>切换文本();<?php-endif;?>} );})(jQuery);</script><?php(电话)}/***根据步骤2显示JavaScript。**@自2.6.0起*/公共函数js_2(){?><script type=“text/javascript”>EndCrop上的函数(坐标){jQuery('#x1').val(坐标.x);jQuery('#y1').val(坐标.y);jQuery('#width').val(coords.w);jQuery('#height').val(坐标.h);}jQuery(函数(){var xinit=<?php echo absint(get_theme_support('custom-header','width'));?>;var yinit=<?php echo absint(get_theme_support('custom-header','height'));?>;var比率=xinit/yinit;var ximg=jQuery('img#upload').width();var yimg=jQuery('img#upload').height();if(yimg<yinit | ximg<xinit){if(ximg/yimg>比率){yinit=yimg;xinit=yinit*比率;}其他{xinit=最大值;yinit=辛尼特/比值;}}jQuery('img#upload').imgAreaSelect({句柄:true,键:true,显示:true,x1:0,y1:0,x2:xinit,y2:伊尼特,<?php(电话)if(!current_theme_supports('custom-header','flex-height')&& ! current_theme_supports(“自定义页眉”、“可变宽度”)) {?>aspectRatio:xinit+':'+yinit,<?php(电话)}if(!current_theme_supports('custom-header','flex-height')){?>最大高度:<?php echo get_theme_support('custom-header','height');?>,<?php(电话)}if(!current_theme_supports('custom-header','flex-width')){?>最大宽度:<?php echo get_theme_support('custom-header','width');?>,<?php(电话)}?>onInit:函数(){jQuery('#width').val(xinit);jQuery('#height').val(yinit);},onSelectChange:函数(img,c){jQuery('#x1').val(c.x1);jQuery('#y1').val(c.y1);jQuery('#width').val(c.width);jQuery('#height').val(c.height);}});} );</script><?php(电话)}/***显示自定义页眉图像页面的第一步。**@自2.1.0起*/公共函数step_1(){$this->process_default_headers();?><div class=“wrap”><h1><?php_e(“自定义标题”);?></h1><?php(电话)if(current_user_can('自定义')){$message=冲刺(/*转换器:%s:自定义程序中标头图像配置的URL*/__('您现在可以在<a href=“%s”>Customizer</a>中管理和实时预览自定义标头。'),admin_url('自定义.php?自动聚焦[control]=header_image'));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车身><?php if(get_custom_header()||display_header_text()):?><tr><th scope=“row”><?php_e(“预览”);?></第>个<td><?php(电话)if($this->adminimagediv_callback){call_user_func($this->admin_image_div_callback);}其他{$custom_header=获取自定义头();$header_image=获取头部图像();if($header_image){$header_image_style='背景图像:url('.esc_url($header_image).');';}其他{$header_image_style='';}if($customheader->width){$header_image_style.='最大宽度:'$custom_header->width。'像素;';}if($custom_header->height){$header_image_style.='高度:'$custom_header->高度。'像素;';}?><div id=“headimg”style=“<?php echo$header_image_style;?>”><?php(电话)if(display_header_text()){$style='style=“color:#'.get_header_textcolor().';”';}其他{$style='style=“display:none;”';}?><h1><a id=“name”class=“displaying-header-text”<?php-echo$style;?>onclick=“return false;”href=“<?php bloginfo('url');?>”tabindex=“-1”><?php博客信息(“name”);?></a></h1><div id=“desc”class=“displaying-header-text”<?php-echo$style;?>><?php博客信息(“描述”);?></div(分频)></div><?php}?></td></tr><?php-endif;?><?php if(current_user_can('upload_files')&&current_theme_supports('custom-header','uploads')):?><tr><th scope=“row”><?php _e(“选择图像”);?></第>个<td><p><?php _e('您可以通过从计算机上传或从媒体库中选择来选择要显示在站点顶部的图像。选择图像后,您将能够裁剪它。');?><br/><?php(电话)if(!current_theme_supports('custom-header','flex-height')&& ! current_theme_supports('custom-header','flex-width')) {打印(/*转换器:1:图像宽度(像素),2:图像高度(像素)*/__('精确<strong>%1$d&times;%2$d像素的图像将用作-is。')<br/>',get_theme_support('自定义标题','宽度'),get_theme_support('自定义标题','高度'));}elseif(current_theme_supports('custom-header','flex-height')){if(!current_theme_supports('custom-header','flex-width')){打印(/*转换器:%s:大小(像素)*/__('图像应至少为%s宽。')。“”,冲刺(/*转换器:%d:自定义页眉宽度*/“<strong>”__(“%d像素”)。”</strong>',get_theme_support('自定义标题','宽度')));}}elseif(current_theme_supports(“自定义标题”、“灵活宽度”)){if(!current_theme_supports('custom-header','flex-height')){打印(/*转换器:%s:大小(像素)*/__('图像高度至少应为%s。')。'',冲刺(/*转换器:%d:自定义页眉高度*/“<strong>”__(“%d像素”)。”</strong>',get_theme_support('自定义标题','高度')));}}if(current_theme_supports('custom-header','flex-height')||current_theme_supports('custom-header','flex-width')) {if(current_theme_supports('custom-header','width')){打印(/*转换器:%s:大小(像素)*/__('建议宽度为%s。')。'',冲刺(/*转换器:%d:自定义页眉宽度*/“<strong>”__(“%d像素”)。”</strong>',get_theme_support('自定义标题','宽度')));}if(current_theme_supports('custom-header','height')){打印(/*转换器:%s:大小(像素)*/__('建议高度为%s。')。'',冲刺(/*转换器:%d:自定义页眉高度*/“<strong>”__(“%d像素”)。”</strong>',get_theme_support(“自定义页眉”、“高度”)));}}?></p><form enctype=“multipart/form data”id=“upload form”class=“wp upload form”method=“post”action=“<?php echo esc_url(add_query_arg('step',2));?>”><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><?php(电话)$modal_update_href=添加查询参数(阵列(“page”=>“custom-header”,“步骤”=>2,“_wpnoce-custom-header-upload”=>wp_create_noce(“custom-hheader-upport”),),admin_url('themes.php'));?><p><label for=“choose-from-library-link”><?php_e('或从媒体库中选择图像:');?></标签><button id=“choose-from-library-link”class=“button”data-update-link=“<?php-echo esc_url($modal_update_href);?>”data-choose=“<?php esc_attr_e('选择自定义标题');?>”data-update=“<?php esc_attr_e('设置为标题');?>”><?php_e(“选择图像”);?></按钮></p></form></td></tr><?php-endif;?></tbody></表格><form method=“post”action=“<?php echo esc_url(add_query_arg('step',1));?>”><?php submit_button(null,“屏幕阅读器文本”,“保存标题选项”,false);?><table class=“form-table”role=“presentation”><t车身><?php if(get_uploaded_header_images()):?><tr><th scope=“row”><?php_e('上传图像');?></第>个<td><p><?php_e('您可以选择一个以前上传的标题,或显示一个随机标题。');?></p><?php(电话)$this->show_header_selector(“已上传”);?></td></tr><?php(电话)endif;if(!空($this->default_headers)):?><tr><th scope=“row”><?php_e(“默认图像”);?></第>个<td><?php if(current_theme_supports('custom-header','uploads')):?><p><?php_e('如果您不想上传自己的图像,可以使用这些很酷的标题之一,也可以随机显示一个。');?></p><?php其他:?><p><?php_e('您可以使用这些很酷的标题之一,也可以在每个页面上随机显示一个标题。');?></p><?php-endif;?><?php(电话)$this->show_header_selector(“默认”);?></td></tr><?php(电话)endif;如果(get_header_image()):?><tr><th scope=“row”><?php_e(“删除图像”);?></第>个<td><p><?php_e('这将删除标题图像。您将无法恢复任何自定义设置。');?></p><?php submit_button(__(“删除页眉图像”),“”,“removeheader”,false);?></td></tr><?php(电话)endif;$default_image=冲刺(get_theme_support('custom-header','default-image'),获取模板目录uri(),获取样式表_目录_uri());if($default_image&&get_header_image()!==$默认图像):?><tr><th scope=“row”><?php_e(“重置图像”);?></第>个<td><p><?php_e('这将恢复原始标题图像。您将无法恢复任何自定义设置。');?></p><?php submit_button(__(“还原原始页眉图像”),“”,“resetheader”,false);?></td></tr><?php-endif;?></tbody></表格><?php if(current_theme_supports('custom-header','header-text')):?><h2><?php_e(“标题文本”);?></h2><table class=“form-table”role=“presentation”><t车身><tr><th scope=“row”><?php_e(“标题文本”);?></第>个<td><p><label><input type=“checkbox”name=“display-header-text”id=“display header-text”<?php已检查(display_header_text());?>/><?php_e('用图像显示标题文本。');?></标签></p></td></tr><tr class=“显示标题文本”><th scope=“row”><?php_e(“文本颜色”);?></第>个<td><p><?php(电话)$default_color=“”;if(current_theme_supports('custom-header','default-text-color')){$default_color=get_theme_support('custom-header','default-text-color');if($default_color&&!str_contains($default颜色,'#')){$default_color=“#”$默认颜色;}}$default_color_attr=$default_color?'data-default-color=“'.esc_attr($default_color).'”“':'';$header_textcolor=display_header_text()?get_header_textcolor():get_theme_support('custom-header','default-text-color');if($header_textcolor&&!str_contains($header _textcolor,'#')){$header_textcolor=“#”$header_textcolor;}echo'<input type=“text”name=“text-color”id=“text-ccolor”value=“'.esc_attr($header_textcolor).'”“'$default_color_attr.'/>';if($default_color){/*转换器:%s:默认文本颜色*/echo'<span class=“description hide-if-js”>'。sprintf(_x('默认:%s','颜色'),esc_html($Default_color))。'</span>';}?></p></td></tr></tbody></表格><?php(电话)endif;/***在自定义标题选项窗体中的提交按钮之前激发。**@自3.1.0起*/do_action('自定义标题选项');wp_nonce_field('自定义页眉选项','自定义页首选项');?><?php submit_button(null,'primary','save-header-options');?></form></div><?php(电话)}/***显示自定义页眉图像页面的第二步。**@自2.1.0起*/公共函数步骤2(){check_admin_referer('custom-header-upload','_wpnonce-custom-header-rupload');if(!current_theme_supports('custom-header','uploads')){wp_die(&D)(“<h1>”__(“出了问题。”)</h1>’。“<p>”__('活动主题不支持上载自定义标题图像。')'</p> ',403);}if(空($_POST)&&isset($_GET['file']){$attachment_id=absint($_GET['file']);$file=获取附加文件($attachment_id,true);$url=wp_get_attachment_image_src($attachment_id,'full');$url=$url[0];}elseif(isset($_POST)){$data=$this->step_2_manage_upload();$attachment_id=$data['attachment _id'];$file=$data['file'];$url=$data['url'];}if(file_exists($file)){列表($width、$height、$type、$attr)=wp_getimagesize($file);}其他{$data=wp_get_attachment_metadata($attachment_id);$height=isset($data['height'])?(int)$data['height']:0;$width=isset($data['width'])?(int)$data['width']:0;取消设置($data);}$max_width=0;//对于flex,除非主题另有说明,否则将显示的图像大小限制为1500px。if(current_theme_supports('custom-header','flex-width')){$最大宽度=1500;}if(current_theme_supports('custom-header','max-width')){$max_width=最大($max_width,get_theme_support('custom-header','max-width'));}$max_width=max($max_width,get_theme_support('custom-header','width'));//如果不支持灵活的高度,并且图像大小正好合适。if(!current_theme_supports('custom-header','flex-height')&& ! current_theme_supports('custom-header','flex-width')&&(int)get_theme_support('custom header','width')===$width&&(int)get_theme_support('custom header','height')===$height) {//添加元数据。if(file_exists($file)){wp_update_attachment_metadata($attachment_id,wp_generate_attachment_cetadata($附件_id,$file));}$this->set_header_image(压缩('url','attachment_id','width','height');/***设置自定义标题或背景图像后过滤附件文件路径。**用于文件复制。**@自2.1.0起**@param string$file文件的路径。*@param int$attachment_id附件id。*/$file=应用过滤器('wp_create_file_in_uploads',$file,$attachment_id);//用于复制。return$this->finished();}elseif($width>$max_width){$oitar=$width/$max_width;$image=wp_crop_image($attachment_id,0中,0中,$宽度,$高度,$max_width,$height/$oitar,错误,str_replace(wp_basename($file),“中型-”。wp_base名称($文件),$文件));if(!$image||iswp_error($image)){wp_die(__('无法处理图像。请返回并重试。'),__('图像处理错误'));}/**该过滤器记录在wp-admin/includes/class-custom-image-header.php中*/$image=应用过滤器('wp_create_file_in_uploads',$image,$attachment_id);//用于复制。$url=str_replace(wp_basename($url),wp_base名称($image),$url;$width=$width/$oitar;$height=$height/$oitar;}其他{美元=1;}?><div class=“wrap”><h1><?php_e('裁剪标题图像');?></h1><form method=“post”action=“<?php echo esc_url(add_query_arg('step',3));?>”><p class=“hide-if-no-js”><?php_e('选择要用作标题的图像部分。');?></p><p class=“hide-if-js”><strong><?php_e('您需要JavaScript来选择图像的一部分。');?></strong></p><div id=“crop_image”style=“position:relative”><img src=“<?php echo esc_url($url);?>”id=“upload”width=“<?php echo esc_attr($width);?>”height=“<?php echo esc_attr($height);?>”alt=“”/></div><input type=“hidden”name=“x1”id=“x1”value=“0”/><input type=“hidden”name=“y1”id=“y1)value=“0”/><input type=“hidden”name=“width”id=“width-”value=“<?php echo esc_attr($width);?>”/><input type=“hidden”name=“height”id=“height“value=”<?php echo esc_attr($height);?>“/><input type=“hidden”name=“attachment_id”id=“attachement_id“value=”<?php echo esc_attr($attachent_id);?>“/><input type=“hidden”name=“oitar”id=“oistar”value=“<?php echo esc_attr($oitar);?>”/><?php if(空($_POST)&&isset($_GET['file'])){?><input type=“hidden”name=“create-new-attachment”value=“true”/><?php}?><?php wp_nonce_field('自定义标题-裁剪-图像');?><p class=“submit”><?php submit_button(__('裁剪和发布'),'主要','提交',false);?><?php(电话)如果(isset($oitar)&&1===$oitar&&(current_theme_supports('custom-header','flex-height')||current_theme_supports('custom-header','flex-width')) {submit_button(__('跳过裁剪,按原样发布图像'),'','跳过裁剪',false);}?></p></form></div><?php(电话)}/***上载要在第二步中裁剪的文件。**@自3.4.0起*/公共函数step_2_manage_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'],__('Image Upload 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-header”,);//保存数据。$attachment_id=wp_insert_attachment($附件,$文件);return compact(“attachment_id”,“file”,“文件名”,“url”,“type”);}/***显示自定义页眉图像页面的第三步。**@自2.1.0起*@since 4.4.0切换到使用wp_get_attachment_url()而不是guid*用于检索标题图像URL。*/公共函数step_3(){check_admin_referer('自定义标题-裁剪-图像');if(!current_theme_supports('custom-header','uploads')){wp_die(&D)(“<h1>”__(“出了问题。”)</h1>’。“<p>”__('活动主题不支持上载自定义标题图像。')'</p> ',403);}if(!空($_POST['skip-stripping'])&& ! current_theme_supports('custom-header','flex-height')&& ! current_theme_supports(“自定义页眉”、“可变宽度”)) {wp(_D)(“<h1>”__(“出了问题。”)</h1>’。“<p>”__(“活动主题不支持灵活大小的标题图像。”)</p> ',403);}如果($_POST['ior']>1){$_POST['x1']=$_POST['x1']*$_POST_['itar'];$_POST['y1']=$_POST['y1']*$_POST_['itar'];$_POST['width']=$_POST['wight']*$_POST_['itar'];$_POST['height']=$_POST['height']*$_POST_['itar'];}$attachment_id=绝对值($_POST['attachment _id']);$original=获取附加文件($attachment_id);$dimensions=$this->get_header_dimensions(阵列(“height”=>$_POST['height'],“宽度”=>$_POST['width'],));$height=$dimensions['dst_height'];$width=$dimensions['dst_width'];if(空($_POST['skip-ripping']){$cropped=wp_crop_image($attachment_id,(int)$_POST['x1'],(int)$_POST['y1'],(int)$_POST['width'],(int)$_POST['height'],$宽度,$高度);}elseif(!空($_POST['create-new-attachment']){$cropped=_copy_image_file($attachment_id);}其他{$cropped=获取附加文件($attachment_id);}if(!$cropped||iswp_error($croped)){wp_die(__('无法处理图像。请返回并重试。'),__('图像处理错误'));}/**该过滤器记录在wp-admin/includes/class-custom-image-header.php中*/$cropped=apply_filters('wp_create_file_in_uploads',$croped,$attachment_id);//用于复制。$attachment=wp_copy_parent_attachment_properties($cropped,$attachement_id,'custom-header');if(!空($_POST['create-new-attachment']){unset($附件['ID']);}//更新附件。$attachment_id=$this->insert_attachment($attach,$cropped);$url=wp_get_attachment_url($attachment_id);$this->set_header_image(compact('url','attachment_id','width','height');//清理。$medium=str_replace(wp_basename($original),'midsize-'。wp_basename($原始),$原始);if(文件存在($medium)){wp_delete_file($medium);}if(空($_POST['create-new-attachment'])&&empty($_POST['skip-stripping']){wp_delete_file($original);}return$this->finished();}/***显示自定义页眉图像页面的最后一步。**@自2.1.0起*/公共函数finished(){$this->updated=true;$this->step_1();}/***根据当前步骤显示页面。**@自2.1.0起*/公共函数admin_page(){if(!current_user_can('edit_theme_options')){wp_die(__('对不起,您不允许自定义标题。'));}$step=$this->step();如果(2===$step){$this->step_2();}elseif(3===$step){$this->step_3();}其他{$this->step_1();}}/***自3.5.0以来未使用。**@自3.4.0起**@param数组$form_fields*@return数组$form_fields*/公共函数attachment_fields_to_edit($form_fields){返回$form_fields;}/***自3.5.0以来未使用。**@自3.4.0起**@param数组$tabs*@return数组$tab*/公共函数filter_upload_tabs($tabs){返回$tabs;}/***从现有上传和默认标题中选择标题图像,*或提供上载的标头数据数组(新数据或媒体库中的数据)。**@自3.4.0起**@param混合了$choice要选择哪个标题图像。允许“random-default-image”的值,*用于在默认图像之间随机循环;'随机上传图像',*用于在上传的图像之间随机循环;默认图像的键*为该主题注册;以及为该主题上传的图像的关键*(图像的附件ID)。或参数数组:attachment_id,*url、宽度、高度。所有都是必需的。*/最终公共函数set_header_image($choice){if(is_array($choice)|| is_object($choice)){$choice=(数组)$choice;if(!isset($choice['attachment_id'])||!isset($choice[“url”]){回报;}$choice['url']=消毒url($choice['url']);$header_image_data=(对象)数组(“attachment_id”=>$choice['attachment _id’],“url”=>$choice['url'],“thumbnail_url”=>$choice[“url”],'height'=>$choice['height'],'width'=>$choice['widthe'],);update_post_meta($choice[‘attachment_id’],‘_wp_attachment_is_custom_header’,get_stylesheet());set_theme_mod(“header_image”,$choice[“url”]);set_theme_mod('header_image_data',$header_mage_data);回报;}if(in_array($choice,数组('remove-header','random-default-image','random-uploadd-image'),true)){set_theme_mod('header_image',$choice);remove_theme_mod('标题图像数据');回报;}$uploaded=get_uploaded_header_images();if($uploaded&&isset($updated[$choice])){$header_image_data=$已上传[$choice];}其他{$this->process_default_headers();if(isset($this->defaultheaders[$choice]){$header_image_data=$this->default_headers[$choice];}其他{回报;}}set_theme_mod(“header_image”,清理url($header_mage_data['url']));set_theme_mod('header_image_data',$header_mage_data);}/***删除标题图像。**@自3.4.0起*/最后一个公共函数remove_header_image(){$this->set_header_image('remove-header');}/***将标题图像重置为主题的默认图像。**如果主题没有默认的标题图像,则此方法不会执行任何操作。**@自3.4.0起*/最终公共函数reset_header_image(){$this->process_default_headers();$default=get_theme_support('custom-header','default-image');if(!$default){$this->remove_header_image();回报;}$default=sprintf($default,get_template_directory_uri(),get_stylesheet_directory _uri));$default_data=数组();foreach($this->default_headers作为$header=>$details){if($details['url']===$default){$default_data=$details;断裂;}}set_theme_mod(“header_image”,$default);set_theme_mod('header_image_data',(object)$default_data);}/***根据当前所选主题支持的内容计算宽度和高度。**@自3.9.0起**@param数组$dimensions*@return数组标头图像的dst_height和dst_width。*/最终公共函数getheader_dimensions($dimensions){$max_width=0;$width=absint($dimensions['width']);$height=absint($dimensions['height']);$theme_height=get_theme_support('custom-header','height');$theme_width=get_theme_support('自定义标题','宽度');$has_flex_width=current_theme_supports('custom-header','flex-width');$has_flex_height=current_theme_supports('custom-header','flex-height');$has_max_width=current_theme_supports('custom-header','max-width');$dst=阵列('dst_height'=>空,“dst_width”=>空,);//对于flex,除非主题另有说明,否则将显示的图像大小限制为1500px。if($has_flex_width){$最大宽度=1500;}if($has_max_width){$max_width=最大($max_width,get_theme_support('custom-header','max-width'));}$max_width=最大值($max_width,$theme_width);if($has_flex_height&&(!$has_flex_width|$width>$max_width)){$dst['dst_height']=绝对值($height*($max_width/$width));}elseif($has_flex_height&&$has_flex_width){$dst['dst_height']=$height;}其他{$dst['dst_height']=$theme_height;}if($has_flex_width&&(!$has_flex_height||$width>$max_width)){$dst['dst_width']=绝对值($width*($max_width/$width));}elseif($has_flex_width&&$has_flex_height){$dst['dst_width']=$width;}其他{$dst['dst_width']=$theme_width;}返回$dst;}/***创建附件“对象”。**@自3.9.0起*@不推荐使用的6.5.0**@param string$cropped裁剪图像URL。*@param int$parent_attachment_id父映像的附件id。*@return array带有附件对象数据的数组。*/最后一个公共函数create_attachment_object($cropped,$parent_attachent_id){_deprecated_function(__METHOD__,'6.5.0','wp_copy_parent_attachment_properties()');$parent=获取post($parent_attachment_id);$parent_url=wp_get_attachment_url($parent->ID);$url=str_replace(wp_basename($parent_url),wp_base名称($cropped),$parent_ url);$size=wp_getimagesize($cropped);$image_type=($size)$size['时间']:'图像/jpeg';$attachment=数组(“ID”=>$parent_attachment_ID,“post_title”=>wp_basename($cropped),“post_time_type”=>$image_type,“guid”=>$url,“context”=>“custom-header”,“post_parent”=>$parent_attachment_id,);return$附件;}/***插入附件及其元数据。**@自3.9.0起**@param array$attachment包含附件对象数据的数组。*@param string$cropped裁剪图像的文件路径。*@return int附件ID。*/最终公共函数insert_attachment($attachment,$cropped){$parent_id=isset($attachment['post_parent'])$附件['post_parent']:空;unset($attachment[“父节点”]);$attachment_id=wp_insert_attachment($附件,$裁剪);$metadata=wp_generate_attachment_metadata($attachment_id,$cropped);//如果这是一个裁剪,请将原始附件ID保存为元数据。if($parent_id){$metadata['attachment_parent']=$parent_id;}/***过滤标题图像附件元数据。**@自3.9.0起**@参见wp_generate_attachment_metadata()**@param array$metadata附件元数据。*/$metadata=apply_filters('wp_header_image_attachment_metadata',$metadata);wp_update_attachment_metadata($attachment_id,$metadata);返回$attachment_id;}/***获取媒体管理器上载的附件,将其裁剪,然后将其另存为*新对象。返回JSON编码的对象详细信息。**@自3.9.0起*/公共函数ajax_header_crop(){check_ajax_referer('image_editor-'.$_POST['id'],'nonce');if(!current_user_can('edit_theme_options')){wp_send_json_error();}if(!current_theme_supports('custom-header','uploads')){wp_send_json_error();}$crop_details=$_POST['cropDetails'];$dimensions=$this->get_header_dimensions(阵列('高度'=>$crop_details[高度'],'width'=>$crop_details['widthe'],));$attachment_id=绝对值($_POST['id']);$cropped=wp_crop_image($attachment_id,(int)$crop_details['x1'],(int)$crop_details['y1'],(int)$crop_details['width'],(int)$crop_details['height'],(int)$dimensions['dst_width'],(int)$dimensions['dst_height']);if(!$cropped||iswp_error($croped)){wp_send_json_error(数组('message'=>__('无法处理图像。请返回并重试。'));}/**该过滤器记录在wp-admin/includes/class-custom-image-header.php中*/$cropped=apply_filters('wp_create_file_in_uploads',$croped,$attachment_id);//用于复制。$attachment=wp_copy_parent_attachment_properties($cropped,$attachement_id,'custom-header');$previous=$this->get_previous_crop($attachment);if($previous){$attachment['ID']=$上一个;}其他{unset($附件['ID']);}$new_attachment_id=$this->insert_attachment($attachment,$cropped);$attachment['attachent_id']=$new_attachment_id;$attachment['url']=wp_get_attachment_url($new_attachent_id);$attachment['width']=$dimensions['dst_width'];$attachment['height']=$dimensions['dst_height'];wp_send_json_success($附件);}/***给定标题图像的附件ID,更新其“上次使用的”*时间戳到现在。**当用户尝试从*媒体管理器,即使他/她没有保存更改。**@自3.9.0起*/公共函数ajax_header_add(){check_ajax_referer('header-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();}$key=“_wp_attachment_custom_header_last_used_”。获取样式表();update_post_meta($attachment_id,$key,time());更新post_meta($attachment_id,'_wp_attachment_is_custom_header',get_stylesheet());wp_send_json_success();}/***给定标题图像的附件ID,将其取消设置为用户已加载*活动主题的标题图像。**当用户单击每个图像旁边的叠加“X”按钮时触发*在Customizer的Header工具中选择。**@自3.9.0起*/公共函数ajax_header_remove(){check_ajax_referer('header-remove','nonce');if(!current_user_can('edit_theme_options')){wp_send_json_error();}$attachment_id=绝对值($_POST['attachment _id']);如果($attachment_id<1){wp_send_json_error();}$key=“_wp_attachment_custom_header_last_used_”。获取样式表();删除post_meta($attachment_id,$key);删除post_meta($attachment_id,'_wp_attachment_is_custom_header',get_stylesheet());wp_send_json_success();}/***通过Customizer保存新的页眉图像后,更新页眉图像附件上最近使用的邮戳。**@自3.9.0起**@param WP_Customize_Manager$WP_Customize自定义管理器。*/公共函数customize_set_last_used($wp_customize){$header_image_data_setting=$wp_customize->get_setting('header_mage_data');if(!$header_image_data_setting){回报;}$data=$header_image_data_setting->post_value();if(!isset($data['attachment_id']){回报;}$attachment_id=$data['attachment _id'];$key=“_wp_attachment_custom_header_last_used_”。获取样式表();update_post_meta($attachment_id,$key,time());}/***获取默认标头图像的详细信息(如果已定义)。**@自3.9.0起**@return数组默认标题图像。*/公共函数get_default_header_images(){$this->process_default_headers();//获取默认图像(如果有)。$default=get_theme_support('自定义标题','默认图像');if(!$default){//如果不是,很容易。返回$this->default_headers;}$default=sprintf($default,get_template_directory_uri(),get_stylesheet_directory _uri));$already_has_default=假;foreach($this->default_headers为$k=>$h){if($h['url']===$default){$already_has_default=真;断裂;}}if($already_has_default){返回$this->default_headers;}//如果一个真实的图像未包含在默认集中,请预先添加它。$header_images=数组();$header_images['default']=数组(“url”=>$default,“thumbnail_url”=>$default,'description'=>'Default',);//这一组的其余部分紧随其后。返回array_merge($header_images,$this->default_headers);}/***获取以前上载的标题图像。**@自3.9.0起**@return array上传了标题图像。*/公共函数get_uploaded_header_images(){$header_images=获取上传的头部图像();$timestamp_key=“_wp_attachment_custom_header_last_used_”。获取样式表();$alt_text_key=“_wp_attachment_image_alt”;foreach($header_images作为&$header_image){$header_meta=获取post_meta($header_图像['attachment_id']);$header_image['timestamp']=设置($header_ meta[$timestamp_key])$header_meta[$timestamp_key]:“”;$header_image['alt_text']=设置($header_ meta[$alt_text_key])$header_meta[$alt_text_key]:“”;}返回$header_images;}/***从同一基本图像中获取上一个裁剪的ID。**@自4.9.0起**@param array$attachment包含裁剪附件对象数据的数组。*@return int | false附件ID(如果存在)。如果没有,则为False。*/公共函数get_previous_crop($attachment){$header_images=$this->get_uploaded_header_mages();//如果没有标题图像,请提早退出。if(空($header_images)){返回false;}$previous=false;foreach($header_images作为$image){if($image['attachment_parent']===$attachment['post_parent']){$previous=$image['attachment_id'];断裂;}}返回$previous;}}

变更日志

版本说明
2.1.0介绍。

用户贡献的笔记

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