feat: pull hierarchical settings dict from post metadata

This commit is contained in:
David Bailey 2023-12-19 10:14:14 +01:00
parent 2ee294b012
commit 40a270059f

View file

@ -108,9 +108,47 @@ class MySQLAdapter {
$post_content);
}
function get_post_by_path($post_path, $with_subposts = true) {
$qry = "SELECT * FROM posts WHERE post_path = ?";
function get_settings_for_path($post_path) {
$qry = "
WITH RECURSIVE settings_data (post_path, post_depth, json_settings) AS (
SELECT post_path, post_path_depth, post_metadata
FROM posts
WHERE post_path = ?
UNION ALL
SELECT posts.post_path, posts.post_path_depth, posts.post_metadata
FROM posts, settings_data
WHERE posts.post_path = SUBSTRING_INDEX(settings_data.post_path, '/', settings_data.post_depth)
AND posts.post_path_depth = settings_data.post_depth - 1
)
SELECT post_depth, json_settings
FROM settings_data
ORDER BY post_depth ASC;
";
$out_settings = [];
$settings_list = $this->_exec($qry, "s", $post_path)->fetch_all(MYSQLI_ASSOC);
foreach($settings_list AS $setting) {
$setting = json_decode($setting['json_settings'], true);
if(!isset($setting) || !isset($setting['settings'])) {
continue;
}
$out_settings = array_merge($out_settings, $setting['settings']);
}
return $out_settings;
}
function get_post_by_path($post_path,
$with_subposts = true, $with_settings = true) {
$qry = "SELECT * FROM posts WHERE post_path = ?";
$post_path = chop($post_path, '/');
$post_data = $this->_exec($qry, "s", $post_path)->fetch_assoc();
@ -121,6 +159,9 @@ class MySQLAdapter {
if($with_subposts) {
$post_data['subposts'] = $this->get_subposts_by_path($post_path);
}
if($with_settings) {
$post_data['settings'] = $this->get_settings_for_path($post_path);
}
return $post_data;
}