[英]pyrocms extending core plugin
我將擴展分析插件。我想要獲得最受歡迎的頁面。 我閱讀了Google 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}
任何人都可以給個建議嗎?
PyroCMS沒有擴展內核中任何內容的功能,當然,復制代碼不是最好的主意。 因此,這就是我要做的:創建您自己的庫,但擴展前一個庫。
因此,您需要在類定義之前包含舊庫的文件,然后執行
class My_lib extends The_lib
不完美,但我猜是最好的選擇。
PS:如果您對lib的擴展充滿信心,為什么不將其提交到PyroCMS存儲庫?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.