Hướng dẫn nâng cấp module tương thích bản 4.0.29 lên 4.2.01 - nukeviet/update GitHub Wiki
Kiểm tra trong file admin.menu.php của module nếu có sử dụng biến $nv_Cache
cần gọi global biến này
Nếu module có sử dụng chức năng comment của hệ thống, cần thêm hai cấu hình perpagecomm
và timeoutcomm
bằng cách thêm vào file action:
$sql_create_module[] = "INSERT INTO " . NV_CONFIG_GLOBALTABLE . " (lang, module, config_name, config_value) VALUES ('" . $lang . "', '" . $module_name . "', 'perpagecomm', '5')";
$sql_create_module[] = "INSERT INTO " . NV_CONFIG_GLOBALTABLE . " (lang, module, config_name, config_value) VALUES ('" . $lang . "', '" . $module_name . "', 'timeoutcomm', '360')";
Chú ý:
- perpagecomm là số bình luận trên một trang
- timeoutcomm là thời gian tính bằng giây giữa hai lần gửi bình luận (bằng 0 thì không giới hạn)
Tại các đoạn có dạng
$(window).load(function () {
Thay lại thành
$(window).on('load', function() {
Tại các chỗ xuất ra captcha trước đó thêm lệnh kiểm tra biến $global_config['captcha_type']
nếu bằng 2 thì xuất thêm ra reCAPTCHA. Ví dụ
Trước khi có reCAPTCHA
if ($global_config['gfx_chk'] > 0) {
$xtpl->assign('GFX_NUM', NV_GFX_NUM);
$xtpl->assign('CAPTCHA_REFRESH', $lang_global['captcharefresh']);
$xtpl->assign('CAPTCHA_REFR_SRC', NV_BASE_SITEURL . NV_ASSETS_DIR . '/images/refresh.png');
$xtpl->assign('N_CAPTCHA', $lang_global['securitycode']);
$xtpl->assign('GFX_WIDTH', NV_GFX_WIDTH);
$xtpl->assign('GFX_HEIGHT', NV_GFX_HEIGHT);
$xtpl->parse('main.content.captcha');
}
Khi có reCAPTCHA
if ($global_config['gfx_chk'] > 0) {
if ($global_config['captcha_type'] == 2) {
$xtpl->assign('RECAPTCHA_ELEMENT', 'recaptcha' . nv_genpass(8));
$xtpl->assign('N_CAPTCHA', $lang_global['securitycode1']);
$xtpl->parse('main.content.recaptcha');
} else {
$xtpl->assign('GFX_NUM', NV_GFX_NUM);
$xtpl->assign('CAPTCHA_REFRESH', $lang_global['captcharefresh']);
$xtpl->assign('CAPTCHA_REFR_SRC', NV_BASE_SITEURL . NV_ASSETS_DIR . '/images/refresh.png');
$xtpl->assign('N_CAPTCHA', $lang_global['securitycode']);
$xtpl->assign('GFX_WIDTH', NV_GFX_WIDTH);
$xtpl->assign('GFX_HEIGHT', NV_GFX_HEIGHT);
$xtpl->parse('main.content.captcha');
}
}
Trong file TPL của module thêm reCAPTCHA
<!-- BEGIN: recaptcha -->
<div class="form-group">
<label class="col-sm-4 control-label">{N_CAPTCHA} <span class="txtrequired">(*)</span></label>
<div class="col-sm-20">
<div class="nv-recaptcha-default"><div id="{RECAPTCHA_ELEMENT}"></div></div>
<script type="text/javascript">
nv_recaptcha_elements.push({
id: "{RECAPTCHA_ELEMENT}",
btn: $('[type="submit"]', $('#{RECAPTCHA_ELEMENT}').parent().parent().parent().parent())
})
</script>
</div>
</div>
<!-- END: recaptcha -->
Xem thêm về cách xuất captcha http://wiki.nukeviet.vn/programming4:thematic:captchathemedefault
Đối với reCAPTCHA, mã trả về sẽ nằm ở khóa g-recaptcha-response
do đó cần thay đổi ví dụ như sau
if ($global_config['captcha_type'] == 2) {
$fcode = $nv_Request->get_title('g-recaptcha-response', 'post', '');
} else {
$fcode = $nv_Request->get_title('fcode', 'post', '');
}
Cần kiểm tra thay thế
DIR_FORUM => $global_config['dir_forum']
NV_UNICKMAX => $global_config['nv_unickmax']
NV_UNICKMIN => $global_config['nv_unickmin']
NV_UPASSMAX => $global_config['nv_upassmax']
NV_UPASSMIN => $global_config['nv_upassmin']
Chú ý: Nếu các hằng trên dùng trong hàm cần kiểm tra gọi global biến $global_config.
Các phần dưới đây nếu không thực hiện cũng không gây lỗi.
Việc này nhằm tối ưu cache, nếu không thực hiện module vẫn hoạt động bình thường.
Tìm trong code module (đặc biệt ở các file `modules/ten-module/funcs/sitemap.php) các phần kiểm tra thời gian cache giống
filemtime(NV_ROOTDIR . '/' . NV_CACHEDIR . '/' . $module_name . '/' . $cacheFile) >= $pa
Nếu có xóa các phần đó, kiểm tra lại biến $pa
theo kiểu $pa = NV_CURRENTTIME - 7200;
, đưa giá trị 7200
thành time-to-live (tham số thứ tư) trong lệnh $nv_Cache->getItem
và $nv_Cache->setItem
.
Ví dụ cụ thể đối với module news:
Mở modules/news/funcs/sitemap.php
tìm
$pa = NV_CURRENTTIME - 7200;
Thay lại thành
$cacheTTL = 7200;
Tìm
if (($cache = $nv_Cache->getItem($module_name, $cacheFile)) != false and filemtime(NV_ROOTDIR . '/' . NV_CACHEDIR . '/' . $module_name . '/' . $cacheFile) >= $pa) {
Thay lại thành
if (($cache = $nv_Cache->getItem($module_name, $cacheFile, $cacheTTL)) != false) {
Tìm
$nv_Cache->setItem($module_name, $cacheFile, $cache);
Thay lại thành
$nv_Cache->setItem($module_name, $cacheFile, $cache, $cacheTTL);
Tìm trong code của module:
themes/' . $module_info['template'] . '/modules/' . $module_file
Thay thế bằng:
themes/' . $module_info['template'] . '/modules/' . $module_info['module_theme']
Tìm trong code của module:
themes/' . $module_info['template'] . '/images/' . $module_file
Thay thế bằng:
themes/' . $module_info['template'] . '/images/' . $module_info['module_theme']
Chức năng rewrite các module từ trước giờ có thể không cần sửa gì vẫn hoạt động tốt, tuy nhiên để tối ưu nhất các nhà phát triển module cần chú ý tại các form có menthod = GET cần chú ý thay đổi giá trị action.
Ví dụ luật rewrite cũ ta dùng form với cấu trúc như sau:
<form action="{DATA.action}" name="form_search" method="get" id="form_search" role="form">
<input type="hidden" name="{NV_LANG_VARIABLE}" value="{NV_LANG_DATA}"/>
<input type="hidden" name="{NV_NAME_VARIABLE}" value="{MODULE_NAME}"/>
</form>
Khi đó khi submit form thì URL có dạng
index.php?language=vi&nv=seek&q=.....
Trong file xử lý ta thường dùng hàm nv_url_rewrite
để rewrite URL trên lại sau đó kiểm tra với giá trị $_SERVER['REQUEST_URI']
nếu sai khác sẽ tiến hành redirect về giá trị đã rewrite. Để tối ưu hơn ta cần thay đổi lại form tìm kiếm như sau:
<form action="{DATA.action}" name="form_search" method="get" id="form_search" role="form">
<!-- BEGIN: no_rewrite -->
<input type="hidden" name="{NV_LANG_VARIABLE}" value="{NV_LANG_DATA}"/>
<input type="hidden" name="{NV_NAME_VARIABLE}" value="{MODULE_NAME}"/>
<!-- END: no_rewrite -->
</form>
Theo đó giá trị {DATA.action}
không phải là {NV_BASE_SITEURL}index.php
mà cần thay đổi lại theo
if (!$global_config['rewrite_enable']) {
$data['action'] = NV_BASE_SITEURL . 'index.php';
} else {
$data['action'] = nv_url_rewrite(NV_BASE_SITEURL . 'index.php?' . NV_LANG_VARIABLE . '=' . NV_LANG_DATA . '&' . NV_NAME_VARIABLE . '=seek', true);
}
Và đoạn
<!-- BEGIN: no_rewrite -->
<input type="hidden" name="{NV_LANG_VARIABLE}" value="{NV_LANG_DATA}"/>
<input type="hidden" name="{NV_NAME_VARIABLE}" value="{MODULE_NAME}"/>
<!-- END: no_rewrite -->
Chỉ xuất ra nếu tắt rewrite
if (!$global_config['rewrite_enable']) {
$xtpl->parse('main.no_rewrite');
}
Kiểm tra lại những chỗ có dùng biến $rewrite_keys cần thay lại thành biến $global_config['rewrite_enable']
Lưu ý đến ngữ nghĩa khi thay
Các module có thể không cần cập nhật, tuy nhiên nếu muốn tùy biến hãy lưu ý:
Thay giá trị
$page_title = $module_info['custom_title'];
Thành
$page_title = $module_info['site_title'];
Và
$page_title = $module_info['funcs'][$op]['func_custom_title'];
Thành
$page_title = $module_info['funcs'][$op]['func_site_title'];
Tại những vị trí thích hợp.
Tìm kiếm trong module các phần có dạng:
header('location...
die();
Thay thế bằng:
nv_redirect_location(...
Ví dụ, thay vì viết:
die(json_encode(array(
'status' => 'error',
'input' => $field_input_name,
'mess' => sprintf($lang_module['field_match_type_error'], $row_f['title'])
)));
Nên viết lại thành:
nv_jsonOutput(array(
'status' => 'error',
'input' => $field_input_name,
'mess' => sprintf($lang_module['field_match_type_error'], $row_f['title'])
));
Tìm trong code module để thay thế theo như trên.
Thay ví lúc trước viết die("Noi dung");
hoặc exit("Noi dung");
Nên chuyển sang viết nv_htmlOutput("Noi dung");
Tìm đến đoạn:
$upload = new NukeViet\Files\Upload
Thêm xuống duới dòng sau:
$upload->setLanguage($lang_global);
Nếu không làm việc này module vẫn chạy, nhưng nếu có lỗi thì ngôn ngữ báo lỗi của phần upload sẽ là tiếng Anh