pyrocms扩展核心插件

时间:2012-07-27 08:26:30

标签: php plugins pyrocms

我要扩展分析插件。我想获得最受欢迎的页面。我读了谷歌api文档。最棒的是,已经有一个php库可以从google analytics api获取数据(太棒了!),而且pyro核心文件中还有一个很好的插件。 我的意思是Plugin_Integration。

现在我想要的是为这个插件添加新方法。这也不是编辑核心文件的好主意。所以,有两种方式:

1-重新实现插件作为共享插件(复制已存在的代码) 2-扩展核心插件。

但不幸的是我不知道如何扩展核心插件。 :(

核心就是这样:

<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Integration Plugin
 *
 * Attaches a Google Analytics tracking piece of code.
 *
 * @author      PyroCMS Dev Team
 * @package     PyroCMS\Core\Plugins
 */
class Plugin_Integration extends Plugin
{

    /**
     * Partial
     *
     * Loads Google Analytic
     *
     * Usage:
     *   {{ integration:analytics }}
     *
     * @return string The analytics partial view.
     */
    function analytics()
    {
        return $this->load->view('fragments/google_analytics', NULL, TRUE);
    }

    /**
     * Visitors
     *
     * Uses Google Analytics data to show page views 
     * and visitors for a given time period
     *
     * Usage:
     *   {{ integration:visitors }}
     *
     * @return array The number of page views and visitors.
     */
    public function visitors()
    {
        $data       = array('visits' => 0, 'views' => 0);
        $start      = $this->attribute('start', '2010-01-01');
        $end        = $this->attribute('end', date('Y-m-d'));
        $refresh    = $this->attribute('refresh', 24); // refresh the cache every n hours

        if (Settings::get('ga_email') and Settings::get('ga_password') and Settings::get('ga_profile'))
        {
            // do we have it? Return it
            if ($cached_response = $this->pyrocache->get('analytics_plugin'))
            {
                return $cached_response;
            }

            else
            {
                try
                {
                    $this->load->library('analytics', array(
                        'username' => Settings::get('ga_email'),
                        'password' => Settings::get('ga_password')
                    ));

                    // Set by GA Profile ID if provided, else try and use the current domain
                    $this->analytics->setProfileById('ga:'.Settings::get('ga_profile'));

                    $this->analytics->setDateRange($start, $end);

                    $visits = $this->analytics->getVisitors();
                    $views  = $this->analytics->getPageviews();

                    if ($visits)
                    {
                        foreach ($visits as $visit)
                        {
                            if ($visit > 0) $data['visits'] += $visit;
                        }
                    }

                    if ($views)
                    {
                        foreach ($views as $view) 
                        {
                            if ($view > 0) $data['views'] += $view;
                        }
                    }

                    // Call the model or library with the method provided and the same arguments
                    $this->pyrocache->write($data, 'analytics_plugin', 60 * 60 * (int) $refresh); // 24 hours
                }

                catch (Exception $e)
                {
                    log_message('error', 'Could not connect to Google Analytics. Called from the analytics plugin');
                }
            }

            return $data;
        }
    }
}

我需要添加像这样的方法,但在一个单独的插件中,它扩展到插件上面

public function most_viewed()
{//the logic}

任何人都可以提出建议吗?

1 个答案:

答案 0 :(得分:1)

PyroCMS没有扩展核心内容的功能,当然复制代码也不是最好的主意。所以这就是我要做的:创建自己的库,但扩展前一个库。

因此,在类定义之前包含旧库的文件,然后执行

class My_lib extends The_lib

不完美,但我认为最好的选择。

PS:如果您对lib的扩展有信心,为什么不将它提交到PyroCMS存储库?