Перейти к основному содержанию
Тема: Примеры рабочих хуков (Прочитано 5016 раз) предыдущая тема - следующая тема

Примеры рабочих хуков

Ху is ху?

Просто, на память.

Самый первык хук о котором вам нужно знать - это хук подключения ваш(-его, -их) файлов с функциями, которые и будут изменять код под ваши нужды в движке SMF.
Итак, хук подключения называется integrate_pre_include.

Пример из установщика мода:

'integrate_pre_include' => '$sourcedir/My-Hook.php',


Чтобы добавить этот хук, или любой другой, в БД, при установке вашего мода, нам нужна функция add_integration_function, а чтобы удалить хук(-и) из БД нам понадобится функция remove_integration_function.

Пример файла установщика хуков:

<?php

// 19.09.2012 by Inter http://tiraspol.me/

// Handle running this file by using SSI.php
if (!defined('SMF') && file_exists(dirname(__FILE__) . '/SSI.php'))
require_once(dirname(__FILE__) . '/SSI.php');
elseif (!defined('SMF'))
die('<b>Error:</b> Cannot install - please verify you put this in the same place as SMF\'s index.php.');
elseif ((SMF == 'SSI') && !$user_info['is_admin'])
die('Admin privileges required.');

$hooks = array(
'integrate_pre_include' => '$boarddir/Sources/Subs-Spoiler.php',
'integrate_bbc_codes' => 'spoiler_bbc_codes',
'integrate_menu_buttons' => 'spoiler_menu_buttons',
'integrate_load_theme' => 'spoiler_load_theme',
'integrate_bbc_buttons' => 'spoiler_bbc_buttons',
);

if (!empty($context['uninstalling']))
$call = 'remove_integration_function';
else
$call = 'add_integration_function';

foreach ($hooks as $hook => $function)
{
$call($hook, $function);
}

Re: Примеры рабочих хуков

Ответ #1
integrate_bbc_buttons

Добавляем кнопку в редактор ББ-кодов в самый конец, во второй ряд кнопок:


// SMF 2.0.2 Subs-Editor.php call_integration_hook('integrate_bbc_buttons', array(&$context['bbc_tags']));
function spoiler_bbc_buttons(&$bbc_tags)
{
global $txt;

$bbc_tags[count($bbc_tags) - 1][] = array(
'image' => 'spoiler',
'code' => 'spoiler',
'before' => '[spoiler]',
'after' => '[/spoiler]',
'description' => $txt['bbc_spoiler']
);
}

Re: Примеры рабочих хуков

Ответ #2
integrate_bbc_codes

Добавляем ббкод в ядро парсера ББ-кодов:


// SMF 2.0.2 Subs.php call_integration_hook('integrate_bbc_codes', array(&$codes));
function spoiler_bbc_codes(&$codes)
{
global $txt;

$ary = array(
array(
'tag' => 'spoiler',
'block_level' => true,
'before' => '<div class="sp-wrap"><div class="sp-body" title="' . $txt['spoiler_title'] . '">',
'after' => '</div></div>',
),
array(
'tag' => 'spoiler',
'type' => 'unparsed_equals', // парсить заголовок запрещено
'validate' => create_function('&$tag, &$data, $disabled', '
global $txt;
if (empty($data))
$data = $txt[\'spoiler_title\'];
// $data = str_replace(array("[", "]"), array("&#0091;", "&#0093;", $data));
'),
// 'test' => '[#]?([A-Za-z][A-Za-z0-9_\-]*)\]',
'block_level' => true,
'before' => '<div class="sp-wrap"><div class="sp-body" title="$1">',
'after' => '</div></div>',
));
$codes = array_merge($codes, $ary);
}

Re: Примеры рабочих хуков

Ответ #3
integrate_actions

File: index.php
Line: 329

Example 1:
function torrent_actions(&$actionArray)
{
$actionArray += array('torrent' => array('Torrent.php', 'Torrent'),);
}


Example 2:
function inter_actions(&$actionArray)
{
$actionArray = array_merge($actionArray, array(
'google' => array('Google.php', 'Google'),
'pmrnews' => array('Pmrnews.php', 'PmrNews'),
'others' => array('Others.php', 'Others'),
'trololo' => array('Trololo.php', 'Trololo'),
));
}

Re: Примеры рабочих хуков

Ответ #4
integrate_general_mod_settings

Хук добавляет настройку в страницу настроек модов:

function article_boards_general_mod_settings(&$config_vars)
{
global $txt;
$txt['title_article_board'] = 'Статьи';
$txt['id_article_board'] = 'ID корневого форума для статей';
$config_vars = array_merge($config_vars, array(
array('title', 'title_article_board'),
array('int', 'id_article_board', 6)
));
}