3

私は、しばらくの間公開されていた WordPress プラグインを更新している最中です。プラグインの「将来の保証」の一環として、プラグインの初期段階で実装された見掛け倒しの命名スキームを取り除くことです。

これらの名前のいずれかが存在するかどうかを確認するアクティベーション フックを追加するだけでよいと考えました。存在する場合は、プラグイン オプション配列を削除し、オプションを再度更新する必要があることをユーザーに知らせる通知を表示します。将来の証明のためにデータをスクラブします。簡単ですね。どうやらそうではありません。

考えられるすべての方法を試しましたが、何をしても、delete_option()、update_option()、add_option()、および get_option() がアクティベーション フック内で機能することはありません。

以下は、アクティベーションフックのセットアップ方法のサンプルです。

// add activation function to get rid of old options and services
function my_activation_hook() {
  global $opts_array;

  //Set variable to false before running foreach loop
  $needs_update = false;

  //Loop through $opts_array['bookmark'] and check for 'badname-' prefix
  foreach($opts_array['bookmark'] as $bkmrk) {
    if(strpos($bkmrk, 'badname-') !== false) {
      //If any have 'badname-' prefix, set variable to true
      $needs_update = true;
    }
  }
  //If variable is true, delete options and set to default
  if($needs_update === true) {

    //Delete the options
    unset($opts_array);
    delete_option('MyPluginOpts');

    //Reset the array to default values
    $opts_array = array(
      'position' => 'below', // below, above, or manual
      'reloption' => 'nofollow', // 'nofollow', or ''
      'targetopt' => '_blank', // 'blank' or 'self'
      'bgimg-yes' => 'yes', // 'yes' or blank
      'mobile-hide' => '', // 'yes' or blank
      'bgimg' => 'shr', // default bg image
      'shorty' => 'b2l',
      'pageorpost' => '',
      'bookmark' => array_keys($bookmarks_opts_data), // pulled from bookmarks-data.php
      'feed' => '1', // 1 or 0
      'expand' => '1',
      'autocenter' => '1',
      'ybuzzcat' => 'science',
      'ybuzzmed' => 'text',
      'twittcat' => '',
      'tweetconfig' => '${title} - ${short_link}', // Custom configuration of tweet
      'defaulttags' => 'blog', // Random word to prevent the Twittley default tag warning
      'warn-choice' => '',
      'doNotIncludeJQuery' => '',
      'custom-mods' => '',
      'scriptInFooter' => '1',
      'vernum' => 'old', //Set to "old" to trigger update notice
    );
    add_option('MyPluginOpts', $opts_array); // Store the option to the database wp_options table in a serialized array
    $opts_array = get_option('MyPluginOpts');// Now reload the variable with stored options from database
  }
}
register_activation_hook(__FILE__, 'my_activation_hook' );
4

1 に答える 1

1

次のコードは、自分のプラグインでいくつかの「見掛け倒しの命名スキーム」を更新する必要があったときに作成したものの要約です:)

WordPress StackExchange でこの質問をした後、ソリューションを構築しました。スレッド全体は読む価値があります。

class MyPlugin {
    var $adminOptionsName = "MyPlugin";

    function MyPlugin() {
    }

    function init() {
        $this->getAdminOptions();
    }

    function getAdminOptions() {

        // New options and values
        $theNewOptions = array(
            'option_1' => 0,
            'option_2' => '',
            'version'  => '1.0'
        );

        // Grab the options in the database
        $theOptions = get_option($this->adminOptionsName);

        // Check if options need update
        if( !isset($theOptions['version']) && !empty($theOptions) ) {
            foreach( $theOptions as $key => $value ) {
                if( $key == 'not_needed' ) {
                    unset( $theOptions[$key] );
                }
                if( $key == 'old_option_1') {
                    $theOptions['option_1'] = $value;
                    unset( $theOptions[$key] );
                }
                // etc...
            }
        }

        // Proceed to the normal Options check
        if (!empty($theOptions)) {
            foreach ($theOptions as $key => $option) {
                $theNewOptions[$key] = $option;
            }
        }

        update_option($this->adminOptionsName, $theNewOptions);

    }
}
于 2012-11-21T00:06:41.327 に答える