如何挂接自定义主题的WordPress升级过程

时间:2017-02-02 作者:jammypeach

我正在尝试创建一个自定义主题升级过程,该过程将有选择地覆盖文件&;备份自定义主题中的旧主题,而不是删除(&;像wordpress通常那样覆盖。

我迷上了pre_set_site_transient_update_themes 要检查来自我们服务器的更新,请显示更新通知并允许wordpress下载新文件-但我不知道如何影响升级的执行。

是否有一个钩子或过滤器可以让我的代码代替wordpress执行升级?

请注意,尽管我更喜欢使用儿童主题&;改为更新父主题,这确实是正确的解决方案,在这种情况下我不能

1 个回复
SO网友:prosti

你有没有检查过类内主题升级程序的挂钩?

File: wp-admin/includes/class-theme-upgrader.php
233:    /**
234:     * Upgrade a theme.
235:     *
236:     * @since 2.8.0
237:     * @since 3.7.0 The `$args` parameter was added, making clearing the update cache optional.
238:     * @access public
239:     *
240:     * @param string $theme The theme slug.
241:     * @param array  $args {
242:     *     Optional. Other arguments for upgrading a theme. Default empty array.
243:     *
244:     *     @type bool $clear_update_cache Whether to clear the update cache if successful.
245:     *                                    Default true.
246:     * }
247:     * @return bool|WP_Error True if the upgrade was successful, false or a WP_Error object otherwise.
248:     */
249:    public function upgrade( $theme, $args = array() ) {
250: 
251:        $defaults = array(
252:            \'clear_update_cache\' => true,
253:        );
254:        $parsed_args = wp_parse_args( $args, $defaults );
255: 
256:        $this->init();
257:        $this->upgrade_strings();
258: 
259:        // Is an update available?
260:        $current = get_site_transient( \'update_themes\' );
261:        if ( !isset( $current->response[ $theme ] ) ) {
262:            $this->skin->before();
263:            $this->skin->set_result(false);
264:            $this->skin->error( \'up_to_date\' );
265:            $this->skin->after();
266:            return false;
267:        }
268: 
269:        $r = $current->response[ $theme ];
270: 
271:        add_filter(\'upgrader_pre_install\', array($this, \'current_before\'), 10, 2);
272:        add_filter(\'upgrader_post_install\', array($this, \'current_after\'), 10, 2);
273:        add_filter(\'upgrader_clear_destination\', array($this, \'delete_old_theme\'), 10, 4);
274:        if ( $parsed_args[\'clear_update_cache\'] ) {
275:            // Clear cache so wp_update_themes() knows about the new theme.
276:            add_action( \'upgrader_process_complete\', \'wp_clean_themes_cache\', 9, 0 );
277:        }
278: 
279:        $this->run( array(
280:            \'package\' => $r[\'package\'],
281:            \'destination\' => get_theme_root( $theme ),
282:            \'clear_destination\' => true,
283:            \'clear_working\' => true,
284:            \'hook_extra\' => array(
285:                \'theme\' => $theme,
286:                \'type\' => \'theme\',
287:                \'action\' => \'update\',
288:            ),
289:        ) );
290: 
291:        remove_action( \'upgrader_process_complete\', \'wp_clean_themes_cache\', 9 );
292:        remove_filter(\'upgrader_pre_install\', array($this, \'current_before\'));
293:        remove_filter(\'upgrader_post_install\', array($this, \'current_after\'));
294:        remove_filter(\'upgrader_clear_destination\', array($this, \'delete_old_theme\'));
295: 
296:        if ( ! $this->result || is_wp_error($this->result) )
297:            return $this->result;
298: 
299:        wp_clean_themes_cache( $parsed_args[\'clear_update_cache\'] );
300: 
301:        return true;
302:    }

相关推荐

About Hooks and Filters

嗯,我很难理解动作和过滤器之间的区别。我确实在代码中使用动作,但我是一个新手,甚至连一点过滤器都不知道。我去过codex,以及NickTheGeek、BillErickson、GaryJones等的多个网站,但没有去过vein。如果你能用简单的话告诉我,并举例说明动作、过滤器和挂钩的基本内容和区别。非常感谢。