简体   繁体   中英

pyrocms extending core plugin

I am going to extend the analytics plugin.I want to get most popular pages. I read the google api docs. The great thing is that there is already a php library to get data from google analytics api ( Great!) and also there is a good plugin in the pyro core files. I mean the Plugin_Integration.

Now what I want is to add new method to this plugin. also this is not good idea to edit the core files. so, there are two ways:

1- re implement the plugins as a shared plugin (duplicating the code which is already there) 2- extending the core plugin.

but unfortunately I don't know how I can extend the core plugin. :(

the pluing is like this in the core:

<?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;
        }
    }
}

i need to add the method like this but in a separate plugin which extends above plugin

public function most_viewed()
{//the logic}

anyone can give a piece advise ?

PyroCMS has no functionality for extending anything in the core, and of course duplicating the code is not the best idea. So here's what I would do: Create your own library, but extend the previous one.

So you include the old library's file before your class definition and then do

class My_lib extends The_lib

Not perfect, but I guess the best option.

PS: if you feel confident about your extension of the lib, why not submit it to the PyroCMS repository?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM