主题_升级程序{}

用于升级/安装主题的核心类。

描述

它旨在从本地zip、远程zip URL或上传的zip文件升级/安装主题。

另请参见

方法

姓名描述
主题_升级程序::bulk_upgrade一次升级多个主题。
主题_升级程序::check_package检查包源是否包含有效的主题。
主题_升级程序::check_parent_Theme_filter检查是否正在安装子主题及其父主题也需要安装。
主题升级程序::current_after升级活动主题后关闭维护模式。
主题_升级程序::current_before在尝试升级活动主题之前打开维护模式。
主题_升级程序::delete_old_Theme在升级期间删除旧主题。
主题_升级程序::hide_activate_preview_actions不向用户显示激活和预览操作。
主题_升级程序::安装安装主题包。
主题_升级程序::install_strings初始化安装字符串。
主题_升级程序::主题_信息获取WP_主题对象。
主题_升级程序::升级升级主题。
主题_升级程序::upgrade_strings初始化升级字符串。

来源

类Theme_Upgrader扩展了WP_Upgrade{/***主题升级产品的结果。**@自2.8.0起*@var数组|WP_Error$result*@请参阅WP_Upgrader::$result*/public$result;/***是否正在批量升级/安装多个主题。**@自2.9.0起*@var bool$bulk*/public$bulk=false;/***新主题信息。**@自5.5.0起*@var数组$new_theme_data**@参见check_package()*/public$new_theme_data=array();/***初始化升级字符串。**@自2.8.0起*/公共函数upgrade_strings(){$this->strings['up_to_date']=__('主题为最新版本。');$this->string['no-package']=__('更新包不可用');/*转换器:%s:包URL*/$this->strings['downloading_package']=sprintf(__('从%s&#8230;下载更新'),'<span class=“code pre”>%s</span>');$this->strings['unpack_package']=__('解包更新&#8230;');$this->strings['remove_old']=__('删除旧版本的主题&#8230;');$this->strings['remove_old_failed']=__('无法删除旧主题。');$this->strings['process_failed']=__('主题更新失败。');$this->strings['process_success']=__('主题更新成功');}/***初始化安装字符串。**@自2.8.0起*/公共函数install_strings(){$this->strings['no_package']=__('安装包不可用。');/*转换器:%s:包URL*/$this->strings['downloading_package']=sprintf(__('从%s&#8230;下载安装包'),'<span class=“code pre”>%s</span>');$this->strings['unpack_package']=__('打开包&#8230;');$this->strings['installing_package']=__('安装主题&#8230;');$this->strings['remove_old']=__('删除旧版本的主题&#8230;');$this->strings['remove_old_failed']=__('无法删除旧主题。');$this->strings['no_files']=__('主题不包含文件。');$this->strings['process_failed']=__('主题安装失败。');$this->strings['process_success']=__('主题安装成功。');/*翻译:1:主题名称,2:主题版本*/$this->strings['process_success_specific']=__('成功安装主题<strong>%1$s%2$s</strong>。');$this->strings['parent_theme_search']=__('此主题需要父主题。检查是否已安装&#8230;');/*翻译:1:主题名称,2:主题版本*/$this->strings['parent_theme_prepare_install']=__('准备安装<strong>%1$s%2$s</strong>&#8230;');/*翻译:1:主题名称,2:主题版本*/$this->strings['parent_theme_currently_installed']=__('父主题<strong>%1$s%2$s</strong>当前已安装。');/*翻译:1:主题名称,2:主题版本*/$this->strings['parent_theme_install_success']=__('成功安装父主题,<strong>%1$s%2$s</strong>。');/*转换器:%s:主题名称*/$this->strings['parent_theme_not_found']=sprintf(__('<strong>找不到父主题。</strong>您需要先安装父主题%s,然后才能使用此子主题。'),'<strng>%s</strong>');/*转换器:%s:主题错误*/$this->strings['current_theme_has_errors']=__('活动主题有以下错误:“%s”。');if(!空($this->skin->overwrite)){if('update-theme'===$this->skin->overwrite){$this->strings['installing_package']=__('更新主题&#8230;');$this->strings['process_failed']=__('主题更新失败');$this->strings['process_success']=__('主题更新成功');}if(“降级主题”===$this->skin->overwrite){$this->strings['installing_package']=__('降级主题&#8230;');$this->strings['process_failed']=__('主题降级失败。');$this->strings['process_success']=__('主题降级成功。');}}}/***检查是否正在安装子主题,以及是否还需要安装其父主题。**连接到'升级程序_post_install'按Theme_Upgrader::install()筛选。**@自3.4.0起**@param bool$安装结果*@param数组$hook_extra*@param数组$child_result*@return布尔*/公共函数check_parent_theme_filter($install_result、$hook_extra、$child_results){//检查是否需要安装父主题。$theme_info=$this->theme_info();if(!$theme_info->parent()){返回$install_result;}$this->skin->反馈(“parent_theme_search”);if(!$theme_info->parent()->errors()){$this->skin->反馈(“parent_theme_currently_installed”,$theme_info->parent()->显示(“Name”),$themes_info->parent(;//我们已经有了主题,失败。返回$install_result;}//我们没有父主题,让我们安装它。$api=主题api(“主题_信息”,阵列(“slug”=>$theme_info->get(“模板”),“字段”=>数组(“sections”=>false,“标记”=>false,),)); // 节省一点带宽。if(!$api |iswp_error($api)){$this->skin->feedback(“parent_theme_not_found”,$theme_info->get(“Template”));//安装后不显示激活或预览操作。add_filter('install_theme_complete_actions',数组($this,'hide_activate_preview_actions]);返回$install_result;}//我们要覆盖的备份所需数据:$child_api=$this->skin->api;$child_success_message=$this->strings[“进程成功”];//覆盖它们。$this->skin->api=$api;$this->strings['process_success_specific']=$this->strings['parent_theme_install_success'];$this->skin->反馈(“parent_theme_prepare_install”,$api->name,$api->version);add_filter('install_theme_complete_actions','__return_false',999);//安装主题后不显示任何操作。//安装父主题。$parent_result=$this->运行(阵列(“package”=>$api->download_link,“目的地”=>get_theme_root(),'clear_destination'=>false,//不要覆盖文件。“clear_working”=>true,));if(is_wp_error($parent_result)){add_filter('install_theme_complete_actions',数组($this,'hide_activate_preview_actions]);}//在父级安装后开始清理。删除过滤器('安装主题完成操作','返回错误',999);//重置孩子的结果和数据。$this->result=$child_result;$this->skin->api=$child_api;$this->strings['process_success']=$child_success-message;返回$install_result;}/***不要向用户显示激活和预览操作。**连接到'安装主题完成操作'筛选依据*安装时Theme_Upgrader::check_parent_Theme_filter()*子主题和安装父主题失败。**@自3.4.0起**@param array$actions预览操作。*@return数组*/公共函数hide_activate_preview_actions($actions){取消设置($actions['activate'],$actions['preview']);返回$actions;}/***安装主题包。**@自2.8.0起*@since 3.7.0添加了`$args`参数,使清除更新缓存成为可选。**@param string$package包的完整本地路径或URI。*@param数组$args{*可选。安装主题包的其他参数。默认空数组。**@type bool$clear_update_cache如果成功,是否清除更新缓存。*默认为true。* }**@return bool|WP_Error如果安装成功则为True,否则为false或WP_Error对象。*/公共函数安装($package,$args=array()){$defaults=数组(“clear_update_cache”=>true,“overwrite_package”=>false,//不覆盖文件。);$parsed_args=wp_parse_args($args,$defaults);$this->init();$this->install_strings();add_filter('upgrader_source_selection',数组($this,'check_package'));add_filter('upgrader_post_install',数组($this,'check_parent_theme_filter'),10,3);if($parsed_args['clear_update_cache']){//清除缓存,以便wp_update_themes()了解新主题。add_action('upgrader_process_complete','wp_clean_themes_cache',9,0);}$this->跑步(阵列(“package”=>$package,“目的地”=>get_theme_root(),“clear_destination”=>$parsed_args['overwrite_package'],“clear_working”=>true,“hook_extra”=>数组(“type”=>“theme”,“action”=>“install”,),));remove_action('upgrader_process_complete','wp_clean_themes_cache',9);remove_filter('upgrader_source_selection',数组($this,'check_package'));remove_filter('upgrader_post_install',数组($this,'check_parent_theme_filter');if(!$this->result||iswp_error($this->result)){返回$this->结果;}//刷新主题更新信息。wp_clean_themes_cache($parsed_args['clear_update_cache']);if($parsed_args['overwrite_package']){/**此操作记录在wp-admin/includes/class-plugin-upgrader.php中*/do_action(“upgrader_overrote_package”,$package,$this->new_theme_data,“theme”);}返回true;}/***升级主题。**@自2.8.0起*@since 3.7.0添加了`$args`参数,使清除更新缓存成为可选。**@param string$theme主题slug。*@param数组$args{*可选。升级主题的其他论据。默认空数组。**@type bool$clear_update_cache如果成功,是否清除更新缓存。*默认为true。* }*@return bool | WP_Error如果升级成功,则返回True,否则返回false或WP_Error对象。*/公共函数升级($theme,$args=array()){$defaults=数组(“clear_update_cache”=>true,);$parsed_args=wp_parse_args($args,$defaults);$this->init();$this->upgrade_strings();//有可用的更新吗?$current=get_site_transient('update_themes');if(!isset($current->response[$theme]){$this->skin->before();$this->skin->set_result(false);$this->skin->error('up_to_date');$this->skin->after();返回false;}$r=$current->响应[$theme];add_filter('upgrader_pre_install',数组($this,'current_before'),10,2);add_filter('upgrader_post_install',数组($this,'current_after'),10,2);add_filter(“upgrader_clear_destination”,数组($this,“delete_old_theme”),10,4);if($parsed_args['clear_update_cache']){//清除缓存,以便wp_update_themes()了解新主题。add_action('upgrader_process_complete','wp_clean_themes_cache',9,0);}$this->跑步(阵列(“package”=>$r['package'],“destination”=>get_theme_root($theme),“clear_destination”=>true,“clear_working”=>true,“hook_extra”=>数组(“主题”=>$theme,'类型'=>'主题','操作'=>'更新',“temp_backup”=>数组(“slug”=>$theme,“src”=>get_theme_root($theme),“dir”=>“themes”,),),));remove_action('upgrader_process_complete','wp_clean_themes_cache',9);remove_filter('upgrader_pre_install',数组($this,'current_before'));remove_filter('upgrader_post_install',数组($this,'current_after'));remove_filter('upgrader_clear_destination',数组($this,'delete_old_theme'));if(!$this->result||iswp_error($this->result)){返回$this->结果;}wp_clean_themes_cache($parsed_args['clear_update_cache']);/**通过删除以下内容,确保任何未来自动更新失败都会触发失败电子邮件*主题更新成功时列表中的最后一个失败通知。*/$past_failure_emails=获取选项('auto_plugin_theme_update_emails',array());if(isset($past_failure_emails[$theme])){取消设置($past_failure_emails[$theme]);更新选项('auto_plugin_theme_update_emails',$past_failure_emails);}返回true;}/***一次升级多个主题。**@自3.0.0起*@since 3.7.0添加了`$args`参数,使清除更新缓存成为可选。**@global string$wp_version WordPress版本字符串。**@param string[]$themes主题slug的数组。*@param数组$args{*可选。同时升级多个主题的其他理由。默认空数组。**@type bool$clear_update_cache如果成功,是否清除更新缓存。*默认为true。* }*@return array[]| false结果数组,如果无法连接到文件系统,则返回false。*/公共函数bulk_upgrade($themes,$args=array()){全局$wp_version;$defaults=数组(“clear_update_cache”=>true,);$parsed_args=wp_parse_args($args,$defaults);$this->init();$this->bulk=true;$this->upgrade_strings();$current=get_site_transient('update_themes');add_filter('upgrader_pre_install',数组($this,'current_before'),10,2);add_filter('upgrader_post_install',数组($this,'current_after'),10,2);add_filter(“upgrader_clear_destination”,数组($this,“delete_old_theme”),10,4);$this->skin->header();//首先连接到文件系统。$res=$this->fs_connect(数组(WP_CONTENT_DIR));如果(!$res){$this->skin->footer();返回false;}$this->skin->bulk_header();/**只有在以下情况下才能启动维护模式:*-运行Multisite并且指定了一个或多个主题,或者*-当前正在使用具有可用更新的主题。*@todo对于多站点,如果可能的话,维护模式应该只适用于单个站点。*/$维护=(is_multisite()&&!空($themes));foreach($themes作为$theme){$maintenance=$maintainment | | get_stylesheet()===$theme | | get_template()==$theme;}if($维护){$this->maintenance_mode(true);}$results=数组();$this->update_count=count($themes);$this->update_current=0;foreach($themes作为$theme){++$this->update_current;$this->skin->theme_info=$this->theme_info($theme);if(!isset($current->response[$theme]){$this->skin->set_result(true);$this->skin->before();$此->皮肤->反馈(“up_to_date”);$this->skin->after();$results[$theme]=true;继续;}//获取zip文件的URL。$r=$current->响应[$theme];if(isset($r['requires'))&&!is_wp_version_compatible($r[“要求”]){$result=新WP_Error('不兼容_wp_required_version',冲刺(/*翻译人员:1:当前WordPress版本,2:新主题版本所需的WordPress版本*/__('您的WordPress版本是%1$s,但新的主题版本需要%2$s。'),$wp_version,$r[“要求”]));$this->skin->before($result);$this->skin->error($result);$this->skin->after();}elseif(isset($r['requires_php'])&&!is_php_version_compatible($r['requires_php']){$result=新WP_Error('不兼容_php_required_version',冲刺(/*转换器:1:当前PHP版本,2:新主题版本所需的PHP版本*/__('服务器上的PHP版本是%1$s,但新主题版本需要%2$s。'),PHP_版本,$r[“需要_php”]));$this->skin->before($result);$this->skin->error($result);$this->skin->after();}其他{add_filter('upgrader_source_selection',数组($this,'check_package'));$result=$this->运行(阵列(“package”=>$r['package'],“destination”=>get_theme_root($theme),“clear_destination”=>true,“clear_working”=>true,“is_multi”=>为真,“hook_extra”=>数组(“主题”=>$theme,“temp_backup”=>数组(“slug”=>$theme,“src”=>get_theme_root($theme),“dir”=>“themes”,),),));remove_filter('upgrader_source_selection',数组($this,'check_package'));}$results[$theme]=$result;//防止凭据身份验证屏幕多次显示。if(false===$result){断裂;}}//结束foreach$themes。$this->maintenance_mode(false);//刷新主题更新信息。wp_clean_themes_cache($parsed_args['clear_update_cache']);/**此操作记录在wp-admin/includes/class-wp-upgrader.php中*/do_操作(“upgrader_process_complete”,$这个,阵列(“action”=>“update”,“type”=>“theme”,“bulk”=>true,“主题”=>$themes,));$this->skin->bulk_footer();$this->skin->footer();//清理我们的钩子,以防其他东西对此连接进行升级。remove_filter('upgrader_pre_install',数组($this,'current_before'));remove_filter('upgrader_post_install',数组($this,'current_after'));remove_filter('upgrader_clear_destination',数组($this,'delete_old_theme'));/**通过删除以下内容,确保任何未来自动更新失败都会触发失败电子邮件*主题更新成功时列表中的最后一个失败通知。*/$past_failure_emails=获取选项('auto_plugin_theme_update_emails',array());foreach($results作为$theme=>$result){//当主题无法手动更新时,维护上次失败通知。如果(!$result||is_wp_error($result)||!isset($past_failure_emails[$theme]){继续;}取消设置($past_failure_emails[$theme]);}更新选项('auto_plugin_theme_update_emails',$past_failure_emails);返回$results;}/***检查包源是否包含有效的主题。**连接到'upgrader_source_selection'按Theme_Upgrader::install()筛选。**@自3.3.0起**@global WP_Filesystem_Base$WP_Filesystem WordPress文件系统子类。*@global string$wp_version WordPress版本字符串。**@param string$source下载包源的路径。*@return string | WP_Error传递的源,或失败时的WP_Error对象。*/公共函数check_package($source){全局$wp_filesystem,$wp_版本;$this->new_theme_data=array();if(is_wp_error($source)){return$来源;}//检查文件夹是否包含有效的主题。$working_directory=str_replace($wp_filesystem->wp_content_dir(),拖尾斜杠(wp_content_dir),$source);if(!is_dir($working_directory)){//Confidence检查,如果上述操作失败,我们不阻止安装。返回$source;}//正确的存档应该在单个子目录中有一个style.css文件。if(!file_exists($working_directory.'style.css')){返回新的WP_Error('不兼容_archive_theme_no_style',$this->strings['incompatible_archive'],冲刺(/*转换器:%s:style.css*/__('主题缺少%s样式表。'),“<code>style.css</code>”));}//Theme_Installer_Skin::do_overwrite()上需要所有这些标头。$info=获取文件数据($working_directory。”样式.css',阵列(“名称”=>“主题名称”,“版本”=>“版本”,“作者”=>“作者”,“模板”=>“模板”,“RequiresWP”=>“至少需要”,“RequiresPHP”=>“Requires PHP”,));if(空($info['Name'])){返回新的WP_Error('不兼容_archive_theme_no_name',$this->strings['incompatible_archive'],冲刺(/*转换器:%s:style.css*/__(“%s样式表不包含有效的主题标题。”),“<code>style.css</code>”));}/**父主题必须包含索引文件:*-经典主题需要/index.php*-块主题需要/templates/index.html或块模板/indexhtml(已弃用5.9.0)。*/如果(空($info['Template'])&&! 文件存在($working_directory.'index.php')&&! file_exists($working_directory.'templates/index.html)&&! file_exists($working_directory.'块模板/index.html')) {返回新的WP_Error('不兼容_体系结构_主题_索引',$this->strings['incompatible_archive'],冲刺(/*翻译人员:1:templates/index.html,2:index.php,3:文档URL,4:Template,5:style.css*/__('缺少模板。独立主题需要有%1$s或%2$s模板文件。子主题需要在%5$s样式表中有%4$s标题。'),“<code>templates/index.html</code>”,“<code>index.php</code>”,__( 'https://developer.wordpress.org/themes/advanced-topics/child-themes(https://developer.wordpress.org/themes/advanced-topics/child-themes)/' ),“<code>模板</code>”,“<code>style.css</code>”));}$requires_php=isset($info['RequiresPHP'])$info['RequiresPHP']:空;$requires_wp=isset($info['RequiresWP'])$info['RequiresWP']:空;如果(!is_php_version_compatible($requires_php)){$error=冲刺(/*翻译:1:当前PHP版本,2:上传主题所需的版本*/__('您服务器上的PHP版本是%1$s,但上传的主题需要%2$s。'),PHP_版本,$要求_php);return new WP_Error('不兼容_php_required_version',$this->strings['incompatible_archive'],$Error);}如果(!iswpversioncompatible($requireswp)){$error=冲刺(/*翻译:1:当前WordPress版本,2:上传主题所需的版本*/__('您的WordPress版本是%1$s,但上传的主题需要%2$s。'),$wp_version,$requires_wp);return new WP_Error('不兼容_WP_required_version',$this->strings['incompatible_archive'],$Error);}$this->new_theme_data=$info;返回$source;}/***在尝试升级活动主题之前打开维护模式。**连接到'升级_预安装'按Theme_Upgrader::upgrade()和筛选*Theme_Upgrader::bulk_upgrade()。**@自2.8.0起**@param bool|WP_Error$response安装开始前的安装响应。*@param array$theme主题参数。*@return bool | WP_Error原始的`$response`参数或WP_Error。*/公共函数current_before($response,$theme){if(iswp_error($response)){返回$response;}$theme=isset($theme['theme'])$主题[主题]:'';//仅在主题处于活动状态时运行。if(get_stylesheet()!==$主题){返回$response;}//更改为维护模式。批量编辑单独处理此问题。if(!$this->批量){$this->maintenance_mode(true);}返回$response;}/***升级活动主题后关闭维护模式。**连接到'升级后安装'按Theme_Upgrader::upgrade()筛选*和Theme_Upgrader::bulk_upgrade()。**@自2.8.0起**@param bool | WP_Error$response安装完成后的安装响应。*@param array$theme主题参数。*@return bool | WP_Error原始的`$response`参数或WP_Error。*/公共函数current_after($response,$theme){if(iswp_error($response)){返回$response;}$theme=isset($theme['theme'])$主题[主题]:'';//仅在主题处于活动状态时运行。if(get_stylesheet()!==$主题){返回$response;}//确保升级后样式表名称没有更改:if(get_stylesheet()===$theme&&$theme!==$此->结果['destination_name']){wp_clean_themes_cache();$stylesheet=$this->result['destination_name'];switch_theme($stylesheet);}//删除维护模式的时间。批量编辑单独处理此问题。if(!$this->批量){$this->maintenance_mode(false);}返回$response;}/***在升级过程中删除旧主题。**连接到“upgrader_clear_destination”按Theme_Upgrader::upgrade()筛选*和Theme_Upgrader::bulk_upgrade()。**@自2.8.0起**@global WP_Filesystem_Base$WP_Filesystem子类**@param bool$已删除*@param字符串$local_destination*@param字符串$remote_destination*@param数组$theme*@return布尔*/公共函数delete_old_theme($removed,$local_destination,$remote_destinaation,$theme){全局$wp_filesystem;if(iswp_error($removed)){return$removed;//传递错误。}if(!isset($theme[主题]){返回$removed;}$theme=$theme[主题];$themes_dir=拖尾斜杠($wp_filesystem->wp_themes_dir($theme));if($wpfilesystem->存在($themesdir.$theme)){if(!$wpfilesystem->delete($themesdir.$theme,true)){返回false;}}返回true;}/***获取主题的WP_Theme对象。**@自2.8.0起*@since 3.0.0添加了`$theme`参数。**@param string$theme主题的目录名。这是可选的,如果没有提供,*将使用最后一个结果中的目录名。*@return WP_Theme | false未提供主题的信息对象或false“$Theme”*并且没有设置最后的结果。*/公共函数theme_info($theme=null){if(空($theme)){if(!空($this->result['destination_name']){$theme=$this->result['destination_name'];}其他{返回false;}}$theme=wp_get_theme($theme);$theme->cache_delete();return$theme;}}

变更日志

版本描述
4.6.0已从wp-admin/includes/class-wp-upgrader.php移到它自己的文件。
2.8.0介绍。

用户贡献的笔记

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