美文网首页
WordPress插件开发笔记(七):添加可持续更新功能

WordPress插件开发笔记(七):添加可持续更新功能

作者: 破旧的大卡车 | 来源:发表于2018-10-27 19:31 被阅读57次

    本节仅作为一个参考. 网上大部分人建议不要将更新的trigger设置在设置页面, 应该在插件启动时候自动执行更新. 下面的过程将是: 用户正常更新插件, 然后通过查看设置页面检查最近更新, 如果希望更新, 点击Upgrade将执行更新.

    首先, 我们在latex2html.php中定义当前版本号

     class active_deactive_class
      {
        protected static $instance;
        const l2h_VER='2.0.0'; // To update edit: includes/admin_settings.php
    

    然后, 在includes/admin_settings.php添加Upgrade小节用以显示具体的更新信息以及Version小节来显示版本信息, 到Welcome页面:

    
          /**
           * Welcome Screen
           */
          // We must declare the welcome_page if you want Submit on welcome page
          register_setting( 'l2h_welcome_page', 'l2h_upgrade_options' );
          add_settings_section(
            'l2h_pluginPage_section_welcome',
            __( 'Let us rock!<hr >', 'val2h' ),
            array( $this, 'l2h_settings_wellcome_callback' ),
            'l2h_welcome_page'
          );
    
          /**
           * Upgrade Settings
           */
          $this->upgrade_options=get_option('l2h_upgrade_options'); //maybe you need initialize the version number for a new install in plugin activte function
          $ver = get_option('l2h_upgrade_options')['VER'];
          $ver_str = $this->l2h_upgrade_vernum($ver, null , true);
          add_settings_section(
            'l2h_upgradefrom_' . $ver_str,
            __( 'Upgrade notes<hr />', 'val2h' ),
            array($this, 'l2h_upgradefrom_'. $ver_str . '_callback'),
            'l2h_welcome_page'
          );
          /**
           * Version section
           */
    
          add_settings_section(
            'l2h_pluginPage_section_version_info',
            __( 'Version infomation<hr >', 'val2h' ),
            null,
            'l2h_welcome_page'
          );
    
          add_settings_field(
            'VER',
            __( 'Current version: ' , 'val2h' ),
            array( $this, 'l2h_disable_input_render' ),
            'l2h_welcome_page',
            'l2h_pluginPage_section_version_info',
            array(
              'field' => 'VER'
            )
          );
    
          add_settings_field(
            'VER-dev',
            __( 'Newest version: ', 'val2h' ),
            array( $this, 'l2h_disable_input_render' ),
            'l2h_welcome_page',
            'l2h_pluginPage_section_version_info',
            array(
              'field' => 'VER-dev'
            )
          );
    

    这里Version小节的两个field是通过l2h_disable_input_render来渲染的. 注意在Upgrade小节, 我们用了一个版本函数l2h_upgrade_vernum, 他的定义如下

        /**
         * The upgrade_ver_num function
         */
        public function l2h_upgrade_vernum($ver, $type=null, $string = false )
        {
          $ver_array = explode('.', $ver);
          if( $string ){
            //means we return the version string (without dots)
            return implode( '', $ver_array );
          }else{
            switch ($type) {
            case 'main':
              $ver_array=[ ++$ver_array[0], 0, 0];
              break;
    
            case 'sub':
              $ver_array=[$ver_array[0], ++$ver_array[1], 0];
              break;
            default:
              $ver_array[2]+=1;
            }
            return implode( '.', $ver_array);
          }
        }
    

    它有三个参数, 第一个$ver是当前版本号, 第二个是$type描述升级类型是main|sub|default. 假设$ver='1.2.1'main将返回2.0.0, 而sub将返回1.3.1, default将返回1.2.2.

    最后, 所有的更新都是通过upgrade小节的call back实现的l2h_upgradefrom_'. $ver_str . '_callback(). 因此, 如果当前版本是1.2.1 要升级成2.0.0则我们的callback函数为

     public function l2h_upgradefrom_121_callback()
        {
          $upgrade_options = get_option( 'l2h_upgrade_options' );
          if( $upgrade_options['VER']>'1.2.1' )
            return;
    
          if( $upgrade_options['upgrade_confirm'] ){
            // Do the upgarde: delete old options and so on
            // We already upgraded, so set the upgrade_confirm to false
              $upgrade_options['upgrade_confirm']=false;
              // Set the new version
              $upgrade_options['VER']= $this->l2h_upgrade_vernum($upgrade_options['VER'], 'main');
              update_option('l2h_upgrade_options', $upgrade_options);
          }else{
            if( $upgrade_options['VER'] == '1.2.1' )
            {
              // display the feature of this update
            }
          }
        }
    

    最后, 我们应该注意到, upgrade_confirm这个值并没有在前面设置, 其实他是放在includes/admin_page.php中的:

           <form action='options.php' method='post'>
    <?php
           if( $this->active_tab == 'welcome' ){
             // This prints out all hidden setting fields
             @settings_fields( 'l2h_welcome_page' );
             echo "<input type='hidden' name='l2h_upgrade_options[upgrade_confirm]' value='1' />";
    
             @do_settings_sections( 'l2h_welcome_page' );
             if( get_option('l2h_upgrade_options')['VER'] < active_deactive_class::l2h_VER ){
               @submit_button('Upgrade');
             }
           }else{
             @settings_fields( 'l2h_setting_page' );
             @do_settings_sections( 'l2h_setting_page' );
             @submit_button();
           }
    ?>
           </form>
    

    下一次, 若要从2.0.0更新, 则只需放在前面的l2h_upgradefrom_121_callback写一个l2h_upgradefrom_121_callback()函数即可.

    相关文章

      网友评论

          本文标题:WordPress插件开发笔记(七):添加可持续更新功能

          本文链接:https://www.haomeiwen.com/subject/xdadtqtx.html