WCDDL - Development How-To (Modules, Hooks, etc)

Status
Not open for further replies.

JmZ

(╯°□°)╯︵ ┻━┻
1,789
2008
729
0
A fork of the other sticky specifically for development information to help you write modules and such.

logo.jpg


THIS THREAD IS ONLY VALID FOR WCDDL3

AS OF NOW WCDDL3 IS IN DEVELOPMENT BUT WILL BE RELEASED TOMORROW MOST LIKELY

Click Here for Main Thread

Modules

Modules are PHP files stored in the WCDDL modules folder, prefixed with 'wcddl_'.
The modules directory is defined in the WCDDL configuration, the default being 'modules/'.

Core

In WCDDL2 you would interact with the core by making a global, $core, and calling methods on that.

In WCDDL3 you retrieve the current Core instance and call methods as required:
PHP:
function myFunction() {
    $core = Core::load();
    header("X-JmZ: " . $core->config('someEpicVariable'));
}
Request Mapping

You can now map requests like so:

PHP:
$downloads = Core::mapRequest('Downloads', array('page', 'type', 'query'));
The above example will return a 'Downloads' instance.
If either of 'page', 'type' or 'query' are set by the user (via _GET or _POST), they will be set in the 'Downloads' instance.

Meaning if I set ?page=5, then $downloads->page will be 5.

Available Classes & Methods

Please see the source, there's way too many to list, especially with info on how they work.

Hooks

WCDDL3 uses a hooking system for modules. You generally write a module in such a way that it consists of only functions and/or classes then hook these functions/classes into the WCDDL core.

For example, you can hook a custom function to be executed when downloads are retrieved, allowing you to modify data before it is returned.

Hook List

WAY TOO MANY HOOKS TO LIST AND EXPLAIN, CHECK THE SOURCE TO SEE WHAT THEY DO.

Complete list of hooks in WCDDL3 is as follows:
Code:
init
DatabaseColumn, (&$query, &$args)
DatabaseRowObject, (&$class, &$query, &$args)
DatabaseRowObjects, (&$class, &$query, &$args)
DatabaseExecute, (&$query, &$args)
DatabaseRow, (&$query, &$args)
DatabaseRows, (&$query, &$args)
CoreGetModules, (&$modulesArray)
DownloadsPreGet, (&$downloadsInstance)
DownloadsGetQuery, (&$sqlQuery)
DownloadsGetWhere, (&$whereClause, &$whereParams)
DownloadsGetFullQuery, (&$sqlQuery)
DownloadsGetRows, (&$rowsArray)
DownloadsPostGet, (&$downloadsInstance)
LogQueryPre, (&$query)
ShowQueriesPost, (&$downloadQueryArray)
CommonFormatUrl, (&$string)
CommonIsEmail, (&$email)
CommonIsUrl, (&$url)
CommonUrlHost, (&$host)
CommonDisplayStr, (&$string)
PagesPre, (&$map)
PagesPost, (&$pageArray)
DownloadQueuePre, (&$downloadInstance)
DownloadDeQueuePre, (&$downloadInstance)
DownloadDeletePre, (&$downloadInstance)
DownloadSavePre, (&$query, &$params)
DownloadAddView, (&$downloadInstance)
DownloadShowTitle, (&$title)
SubmitConstruct, (&$submitInstance)
SubmitPre, (&$submitInstance)
SubmitValidation, (&$submitInstance)
SubmitDownload, (&$download)
SubmitFilterPre, (&$submitInstance)
SubmitFilterValidate, (&$valid)
SiteSavePre, (&$siteInstance)
SiteSave, (&$query, &$params)
SiteGetListPre, (&$siteInstance)
SiteGetList, (&$query)
SiteWhitelist, (&$url)
SiteWhitelistRemove, (&$url)
SiteBlacklistRemove, (&$url)
SiteBlacklist, (&$url)
SiteIsWhitelisted, (&$url)
SiteIsBlacklisted, (&$url)
AdminInit, (&$adminInstance)
AdminHandleContent, (&$go)
AdminAuthenticatePre
AdminAuthenticateFailure
AdminAuthenticateSuccess
How to Hook

Hook your custom function/method using the Core::hook method.

Example:
PHP:
Core::load()->hook('SomeHook', 'myFunction'); // To hook a function
Core::load()->hook('SomeHook', array('myClass', 'someMethod'); // To hook a method of a class
// See the hooklist to know how many params your method/func needs to take
Complete Hooking Example

Example:
PHP:
function JmZ($mapping) {
    // $mapping is an array of page link pattern maps
    // e.g. array('type', '<a href="/index.php?type=#type#&page=#page#">#page#</a>')
    // meaning if _GET/_POST 'type' is set, this html will be used for page links
    // You could SEO it here or have the user alter their wcfg.php
    // wcfg.php contains constants such as WCDDL_PAGES_TYPE with the above html
    foreach($mapping as $mapKey => $map) {
        if($map[0] == 'type')
            $mapping[$mapKey][1] = '<a href="/#type#-downloads-#page#.html">#page#</a>';
    }
    // NOTICE we don't return, $mapping is passed by reference so alters the original when changed
}
Core::load()->hook('PagesPre', 'JmZ');
 
Last edited:
18 comments
lol my bad. I was replying in the other stickie thread looking for info not knowing this thread existed. This has a list of all the hooks and is far more what I was after. I can't believe I didn't see this thread before confusing all the kids in the other thread with php talk.
 
Hook Request:
AdmnLogin - can be used to call check for messages, brute force login prevention or failed login attempts for security, recording last login etc.
Called just after the password is checked.
 
Added 3 hooks for that.

AdminAuthenticatePre
Called within authenticate() before anything else.

AdminAuthenticateFailure
Called when authentication fails, nothing is passed.

AdminAuthenticateSuccess
Called when authentication succeeds, nothing is passed.
 
Looking forward to the final release of this. Have been out of the scene for a long time and plan to rebuild EViLDDL using WCDDL v3 instead of DDLCMS like last time. Just hoping a lot of the mods are updated as I'm very much out of practice with PHP, not touched anything for around two years now :-/
 
Let see if i understand whow this works

PHP:
<?php
// Backlink check
// Coded by JmZ
// LOLLERSKATES
//added looking in download page by moi

if(!defined("WCDDL_GUTS"))
        exit;

// Set the following to true to enable this mod
// Be sure to change yoursite.com below
$modEnabled = false;

function backlinkCheck($submit) {
    // Change the domain below to yours
    $myURL = 'yoursite.com';
    // FGC can be slow, replace with curl if you want
    // Also, this is only a simple check so not always reliable
    $get = file_get_contents($submit->surl);
    if(!preg_match('#href="http://(www\.)?' . $myURL . '/?"#i', $get))
    $get = file_get_contents($submit->url[0]);
    if(!preg_match('#href="http://(www\.)?' . $myURL . '/?"#i', $get))
        $submit->error = 'No backlink detected.';
}
if($modEnabled)
    Core::load()->hook('SubmitValidation', 'backlinkCheck');
 
I want to create a custom field in the downloads table , I updated the table and have also written the function but I cant understand what exactly to hook so that it takes the sql query of the new field .
How to change the query and params in DownloadsSavePre before it is executed ?
 
I didn't code that specific hook as well as I could have, so it may seem a bit hacky but here's how:

- Hook into DownloadsSavePre
- Your function will be given the query string and the parameters
- ($sql, Array $params)

Basically you'd have to replace some common substring with the original and your new fields.

Like so:
PHP:
$sql = str_replace(') VALUES', ',d, e, f) VALUES', $sql);

So say our query is:
INSERT INTO wcddl_downloads (a,b,c) VALUES (:a, :b, :c)

The result of replacing ') VALUES' would be:
INSERT INTO wcddl_downloads (a,b,c,d,e,f) VALUES (:a, :b, :c)

Then do the same for the values:
PHP:
$sql = str_replace('VALUES (', 'VALUES (:d, :e, :f, ', $sql);

Leaving you with:
INSERT INTO wcddl_downloads (a, b, c, d, e, f) VALUES (:a, :b, :c, :d, :e, :f)

Remember all fields must be prefixed with 'd.', e.g. 'd.title' rather than 'title'.

When I get around to it, i'll update it slightly to have better hook handling for this.
 
3.1 has been uploaded now onto github.

The only changes are dev related.

Queries generated, simplified

Certain queries are now generated and have their own hooks. The following methods were introduced:
PHP:
Database::insert($table, $map[, $returnID]);
Database::update($table, $map[, $criteria]);
Database::delete($table[, $map]);
// Examples of usage:
Database::insert(WCDDL_DB_PREFIX . 'whitelist', array(
     'url' => $someSite,
));
// The above will insert a row into the whitelist table

Hooks changed and added

The following hooks now exist:
Code:
DownloadQueueInsert // Called when a download is added to the queue, array of params is passed, see Database::insert() usage
DownloadDeQueueDelete // Called when a queue entry is removed, params/criteria passed
DownloadDeleteDelete // Called when a download is deleted, params passed, see Database::delete() usage
DownloadSaveUpdate // Called when a download is updated in the db, see Database::update() usage
DownloadSaveInsert // Same as above but insertions, not updates
SiteSaveUpdate // Called when a site is updated in the db
SiteSaveInsert // Called when a site is added to the db

An example

In your modules now, if you'd like to alter one of these queries, you simply change the params.

So say you added a field to the downloads table, you hook into DownloadSaveUpdate & DownloadSaveInsert, then add your field to the parameters array.

Meaning:
PHP:
function test($map) {
    $map['myField'] = 'someValue';
}
Core::load()->hook('DownloadSaveInsert', 'test');
 
Status
Not open for further replies.
Back
Top