myUrlHandlers/inc/class.myurlhandlers.php

78 lines
2.1 KiB
PHP
Raw Normal View History

2022-08-15 10:52:22 +00:00
<?php
2022-11-26 22:43:34 +00:00
/**
* @brief myUrlHandlers, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Alex Pirine and contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2022-08-15 10:52:22 +00:00
class myUrlHandlers
{
2022-11-26 22:43:34 +00:00
private static $defaults = [];
private static $url2post = [];
private static $post_adm_url = [];
2022-08-15 10:52:22 +00:00
2022-11-26 22:43:34 +00:00
public static function init()
2022-08-15 10:52:22 +00:00
{
# Set defaults
2022-11-26 22:43:34 +00:00
foreach (dcCore::app()->url->getTypes() as $k => $v) {
2022-08-15 10:52:22 +00:00
if (empty($v['url'])) {
continue;
}
2022-11-26 22:43:34 +00:00
$p = '/' . preg_quote($v['url'], '/') . '/';
$v['representation'] = str_replace('%', '%%', $v['representation']);
$v['representation'] = preg_replace($p, '%s', $v['representation'], 1, $c);
2022-08-15 10:52:22 +00:00
if ($c) {
self::$defaults[$k] = $v;
}
}
2022-11-26 22:43:34 +00:00
foreach (dcCore::app()->getPostTypes() as $k => $v) {
2022-08-15 10:52:22 +00:00
self::$url2post[$v['public_url']] = $k;
2022-11-26 22:43:34 +00:00
self::$post_adm_url[$k] = $v['admin_url'];
2022-08-15 10:52:22 +00:00
}
# Read user settings
$handlers = (array) @unserialize(dcCore::app()->blog->settings->myurlhandlers->url_handlers);
2022-11-26 22:43:34 +00:00
foreach ($handlers as $name => $url) {
self::overrideHandler($name, $url);
2022-08-15 10:52:22 +00:00
}
}
2022-11-26 22:43:34 +00:00
public static function overrideHandler($name, $url)
2022-08-15 10:52:22 +00:00
{
if (!isset(self::$defaults[$name])) {
return;
}
2022-11-26 22:43:34 +00:00
dcCore::app()->url->register(
$name,
$url,
sprintf(self::$defaults[$name]['representation'], $url),
self::$defaults[$name]['handler']
);
2022-08-15 10:52:22 +00:00
2022-11-26 22:43:34 +00:00
$k = self::$url2post[self::$defaults[$name]['url'] . '/%s'] ?? '';
2022-08-15 10:52:22 +00:00
if ($k) {
2022-11-26 22:43:34 +00:00
dcCore::app()->setPostType($k, self::$post_adm_url[$k], dcCore::app()->url->getBase($name) . '/%s');
2022-08-15 10:52:22 +00:00
}
}
public static function getDefaults()
{
2022-11-26 22:43:34 +00:00
$res = [];
foreach (self::$defaults as $k => $v) {
2022-08-15 10:52:22 +00:00
$res[$k] = $v['url'];
}
2022-11-26 22:43:34 +00:00
2022-08-15 10:52:22 +00:00
return $res;
}
}