您的位置:首页 > 其它

LAMP开发精要(4):Wordpress 插件示例之二-对示例一的重构

2010-04-07 17:42 393 查看
    示例一已经完全可以工作了,这个在 WP 2.3.3 和 2.9.1 版本中已经得到证实。

    但示例一有几个缺陷。

    由于 WP 的插件众多,全世界有很多程序员包括我们中国的程序员,都在为它开发插件,这些插件往往被一起安装在一套 WP 系统中协调工作,这未免会产生冲突的情况。

    为了隔离各个插件的代码,我们需要想一些办法。而把插件代码封装在一个类结构中是个行之有效的方法之一。正确应用一些检测函数,可以保证只要插件类名不冲突,类所包含的成员函数就不会与其它插件冲突。这大大增强了插件代码的健壮性。而且插件类名是否冲突也作为代码容错检测的一部分,使系统不至于出现严重的崩溃。

    请参看下面的一个完整的 WP 插件代码,是参考以上考虑,对示例一的代码的完全重构。

    <?php

    /*

      Plugin Name: MyCopyright

      Plugin URI: http://www.why100000.com

      Version: 0.1

      Author: 网眼(张庆-陕西西安-开开工作室-http://www.myopenit.cn)

      Author URI: http://blog.why100000.com

      Description: 把字符串“<!--mycopyrigth-->”替换为版权信息:show copyright once there are letters match “<!--mycopyrigth-->”.

      you should config your copyright information in this file,with this verison.

    */

    if (!class_exists("why100000_Plugin"))

    {

      class why100000_Plugin

      {

        //构造函数

        function why100000_Plugin()

        {

        }

        //加到“设置”主菜单下

        function why100000_add_options_page()

        {

          add_options_page('MyCopyright', 'MyCopyright-Config', 8, 'mycopyright.php', array(&$this, 'why100000_myCopyright_mainpage'));

        }

        function why100000_myCopyright_mainpage()

        {

        ?>

          <form name="formamt" method="post" action="options.php">

            <?php wp_nonce_field('update-options') ?>

            <div class="wrap">

              MyCopyright is active.<br><br>

              <h2>MyCopyright Config (版权插件配置)</h2>

              <div id="msgall">

                <fieldset class="options">

                  <legend>keyword(原字符串):</legend>

                  <p>

                  <textarea style="width: 80%;" rows="5" cols="40" name="why100000_keyword"><?php echo get_option("why100000_keyword");?></textarea>

                  </p>

                </fieldset>

                <fieldset class="options">

                  <legend>replacement(代替的字符串):</legend>

                  <p>

                  <textarea style="width: 80%;" rows="5" cols="40" name="why100000_replace"><?php echo get_option("why100000_replace");?></textarea>

                  </p>

                </fieldset>

              </div>

              <p class="submit">

                <input type="submit" value="<?php _e('Update Options »') ?>" name="update_message"/>

              </p>

              <input type="hidden" name="action" value="update">

              <input type="hidden" name="page_options" value="why100000_keyword,why100000_replace">

              </div>

              </form>

        <?php

        }

        //插件第一次被“启用”时执行,作为初始值保存到表wp_options中

        function why100000_copyright_install()

        {

          add_option("why100000_keyword", "<!--mycopyright-->");

          add_option("why100000_replace", "我的版本");

        }

        //WP执行the_content函数时,调用该函数,对文章内容进行过滤

        function why100000_showcopyright($content)

        {

          $search = get_option("why100000_keyword");  //"<!--mycopyright-->";

          $replace= get_option("why100000_replace");  //代替的字符串;

          $content= str_replace($search, $replace, $content);

          return $content;

        }

      }

    }

    if (class_exists("why100000_Plugin"))

    {

      $MyPlugin = new why100000_Plugin();

    }

    if (isset($MyPlugin))

    {

      add_action('activate_mycopyright/mycopyright.php', array(&$MyPlugin, 'why100000_copyright_install'));

      add_action('admin_menu', array(&$MyPlugin, 'why100000_add_options_page'));

      add_filter('the_content', array(&$MyPlugin, 'why100000_showcopyright'));

    }

    ?>

    于是,我们归纳出一个相对完善的 WP 插件代码书写构架:

    <?php

      /* 插件注释部分...... */

      /* 插件代码部分: */

      /* 插件类定义: */

      if (!class_exists("插件类名"))

      {

        class 插件类名

        {

          /* 全部插件函数...... */

        }

      }

      /* 实例化插件类: */

      if (class_exists("插件类名"))

      {

        $MyPlugin = new 插件类名();

      }

      /* 定义插件事件函数: */

      if (isset($MyPlugin))

      {

        add_action('activate_....php', array(&$MyPlugin, ...));

        add_action('admin_menu', array(&$MyPlugin, ...));

        add_filter('the_content', array(&$MyPlugin, ...));

      }

    ?>

    作者:张庆(网眼) 2010-4-5

    来自“网眼视界”:http://blog.why100000.com

    “十万个为什么”电脑学习网:http://www.why100000.com
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息