diff --git a/system/admin/admin.php b/system/admin/admin.php index 0c70f03..f7524d0 100644 --- a/system/admin/admin.php +++ b/system/admin/admin.php @@ -445,13 +445,17 @@ function edit_content($title, $tag, $url, $content, $oldfile, $revertPost, $publ rebuilt_cache('all'); clear_post_cache($dt, $post_tag, $post_url, $oldfile, $category, $type); - if ($oldfile != $newfile) { + $old_filename = pathinfo($oldfile, PATHINFO_FILENAME); + $old_ex = explode('_', $old_filename); + $old_url = $old_ex[2]; + + if ($old_url != $post_url) { if (file_exists($viewsFile)) { - $views = json_decode(file_get_contents($viewsFile), true); - $arr = replace_key($views, $oldfile, $newfile); - file_put_contents($viewsFile, json_encode($arr, JSON_UNESCAPED_UNICODE), LOCK_EX); + $views = json_decode(file_get_contents($viewsFile), true); + $arr = replace_key($views, 'post_' . $old_url, 'post_' . $post_url); + save_json_pretty($viewsFile, $arr); } - } + } if ($destination == 'post') { if(!empty($revertPost)) { @@ -674,38 +678,52 @@ function edit_page($title, $url, $content, $oldfile, $revertPage, $publishDraft, } else { $pu = $post_url; } + + $old_filename = pathinfo($oldfile, PATHINFO_FILENAME); + $old_ex = explode('.', $old_filename); + if (isset($old_ex[1])) { + $old_url = $old_ex[1]; + } else { + $old_url = $old_filename; + } + + rebuilt_cache('all'); + clear_page_cache($post_url); + if (!empty($static)) { $posturl = site_url() . $static .'/'. $pu; + + if ($old_url != $pu) { + if (file_exists($viewsFile)) { + $views = json_decode(file_get_contents($viewsFile), true); + $arr = replace_key($views, 'subpage_' . $static .'.'. $old_url, 'subpage_' . $static .'.'. $pu); + save_json_pretty($viewsFile, $arr); + } + } + } else { $posturl = site_url() . $pu; - } - rebuilt_cache('all'); - clear_page_cache($post_url); - - if ($oldfile != $newfile) { - if (file_exists($viewsFile)) { - $views = json_decode(file_get_contents($viewsFile), true); - $arr = replace_key($views, $oldfile, $newfile); - file_put_contents($viewsFile, json_encode($arr, JSON_UNESCAPED_UNICODE), LOCK_EX); - } - - if (empty($static)) { + if ($old_url != $pu) { + if (file_exists($viewsFile)) { + $views = json_decode(file_get_contents($viewsFile), true); + $arr = replace_key($views, 'page_' . $old_url, 'page_' . $pu); + save_json_pretty($viewsFile, $arr); + } + $sPage = find_subpage($pu); - $oldSub = 'content/static/' . pathinfo($oldfile, PATHINFO_FILENAME); - $newSub = 'content/static/' . pathinfo($newfile, PATHINFO_FILENAME); if (!empty($sPage)) { foreach ($sPage as $sp) { if (file_exists($viewsFile)) { $views = json_decode(file_get_contents($viewsFile), true); - $arr = replace_key($views, $oldSub . '/' . $sp->md, $newSub . '/' . $sp->md); - file_put_contents($viewsFile, json_encode($arr, JSON_UNESCAPED_UNICODE), LOCK_EX); + $arr = replace_key($views, 'subpage_' . $old_url . '.' . $sp->slug, 'subpage_' . $pu . '.' . $sp->slug); + save_json_pretty($viewsFile, $arr); } } - } + } } - - } + + } if ($destination == 'post') { if(!empty($revertPage)) { @@ -1012,7 +1030,7 @@ function find_draft_page($static = null) $post->body = MarkdownExtra::defaultTransform(remove_html_comments($content)); if (config('views.counter') == 'true') { - $post->views = get_views($post->file); + $post->views = get_views('page_' . $post->slug, $post->file); } else { $post->views = null; } @@ -1082,7 +1100,7 @@ function find_draft_subpage($static = null, $sub_static = null) $post->body = MarkdownExtra::defaultTransform(remove_html_comments($content)); if (config('views.counter') == 'true') { - $post->views = get_views($post->file); + $post->views = get_views('subpage_' . $post->parentSlug .'.'. $post->slug, $post->file); } else { $post->views = null; } @@ -1518,61 +1536,16 @@ function reorder_pages($pages = null) $num = str_pad($i, 2, 0, STR_PAD_LEFT); $arr = explode('.' , $fn); if (isset($arr[1])) { - - $oldSub = find_subpage($arr[1]); - rename ($dir . $p, $dir . $num . '.' . $arr[1] . '.md'); - - if (file_exists($viewsFile)) { - $views = json_decode(file_get_contents($viewsFile), true); - $mod = replace_key($views, $dir . $p, $dir . $num . '.' . $arr[1] . '.md'); - file_put_contents($viewsFile, json_encode($mod, JSON_UNESCAPED_UNICODE), LOCK_EX); - } - if (is_dir($dir . $fn)) { rename($dir . $fn, $dir . $num . '.' . $arr[1]); - - if (!empty($oldSub)) { - foreach ($oldSub as $sp) { - if (file_exists($viewsFile)) { - $views = json_decode(file_get_contents($viewsFile), true); - $mod = replace_key($views, $sp->file, $dir . $num . '.' . $arr[1] . '/' . $sp->md); - file_put_contents($viewsFile, json_encode($mod, JSON_UNESCAPED_UNICODE), LOCK_EX); - } - } - } - } - } else { - - $oldSub = find_subpage($fn); - rename($dir . $p, $dir . $num . '.' . $fn . '.md'); - - if (file_exists($viewsFile)) { - $views = json_decode(file_get_contents($viewsFile), true); - $mod = replace_key($views, $dir . $p, $dir . $num . '.' . $fn . '.md'); - file_put_contents($viewsFile, json_encode($mod, JSON_UNESCAPED_UNICODE), LOCK_EX); - } - if (is_dir($dir . $fn)) { - rename($dir . $fn, $dir . $num . '.' . $fn); - - if (!empty($oldSub)) { - foreach ($oldSub as $sp) { - if (file_exists($viewsFile)) { - $views = json_decode(file_get_contents($viewsFile), true); - $mod = replace_key($views, $sp->file, $dir . $num . '.' . $fn . '/' . $sp->md); - file_put_contents($viewsFile, json_encode($mod, JSON_UNESCAPED_UNICODE), LOCK_EX); - } - } - } - + rename($dir . $fn, $dir . $num . '.' . $fn); } - } - $i++; } @@ -1593,18 +1566,8 @@ function reorder_subpages($subpages = null) $arr = explode('.' , $fn); if (isset($arr[1])) { rename ($dir . $sp, $dn . $num . '.' . $arr[1] . '.md'); - if (file_exists($viewsFile)) { - $views = json_decode(file_get_contents($viewsFile), true); - $mod = replace_key($views, $dir . $sp, $dn . $num . '.' . $arr[1] . '.md'); - file_put_contents($viewsFile, json_encode($mod, JSON_UNESCAPED_UNICODE), LOCK_EX); - } } else { rename($dir . $sp, $dn . $num . '.' . $fn . '.md'); - if (file_exists($viewsFile)) { - $views = json_decode(file_get_contents($viewsFile), true); - $mod = replace_key($views, $dir . $sp, $dn . $num . '.' . $fn . '.md'); - file_put_contents($viewsFile, json_encode($mod, JSON_UNESCAPED_UNICODE), LOCK_EX); - } } $i++; diff --git a/system/htmly.php b/system/htmly.php index 468bc16..b93025f 100644 --- a/system/htmly.php +++ b/system/htmly.php @@ -2532,24 +2532,21 @@ get('/post/:name', function ($name) { if (!login()) { file_cache($_SERVER['REQUEST_URI']); } + } else { + add_view('post_' . $name); + if (!login()) { + file_cache($_SERVER['REQUEST_URI']); + } } $post = find_post(null, null, $name); if (is_null($post)) { - not_found(); + not_found('post_' . $name); } else { $current = $post['current']; } - if (config("views.counter") == "true") { - add_view($current->file); - - if (!login()) { - file_cache($_SERVER['REQUEST_URI']); - } - } - $author = new stdClass; $author->url = $current->authorUrl; $author->name = $current->authorName; @@ -3047,12 +3044,17 @@ get('/:static', function ($static) { if (!login()) { file_cache($_SERVER['REQUEST_URI']); } + } else { + add_view('page_' . $static); + if (!login()) { + file_cache($_SERVER['REQUEST_URI']); + } } $post = find_page($static); if (!$post) { - not_found(); + not_found('page_' . $static); } if (array_key_exists('prev', $post)) { @@ -3068,13 +3070,6 @@ get('/:static', function ($static) { } $post = $post['current']; - - if (config("views.counter") == "true") { - add_view($post->file); - if (!login()) { - file_cache($_SERVER['REQUEST_URI']); - } - } $vroot = rtrim(config('views.root'), '/'); @@ -3335,13 +3330,28 @@ get('/:static/:sub', function ($static, $sub) { $redir = site_url(); header("location: $redir", TRUE, 301); } + + if (config("views.counter") != "true") { + if (!login()) { + file_cache($_SERVER['REQUEST_URI']); + } + } else { + add_view('subpage_' . $static.'.'.$sub); + if (!login()) { + file_cache($_SERVER['REQUEST_URI']); + } + } $parent_post = find_page($static); if (!$parent_post) { - not_found(); + not_found('subpage_' . $static.'.'.$sub); } $post = find_subpage($static, $sub); + if (!$post) { + not_found('subpage_' . $static.'.'.$sub); + } + if (array_key_exists('prev', $post)) { $prev = $post['prev']; } else { @@ -3354,18 +3364,7 @@ get('/:static/:sub', function ($static, $sub) { $next = array(); } - if (!$post) { - not_found(); - } $post = $post['current']; - - if (config("views.counter") == "true") { - add_view($post->file); - } - - if (!login()) { - file_cache($_SERVER['REQUEST_URI']); - } $vroot = rtrim(config('views.root'), '/'); @@ -3400,6 +3399,7 @@ get('/:static/:sub', function ($static, $sub) { 'breadcrumb' => '' . config('breadcrumb.home') . ' » ' . $parent_post['current']->title . ' » ' . $post->title, 'p' => $post, 'static' => $post, + 'parent' => $parent_post, 'prev' => static_prev($prev), 'next' => static_next($next), 'type' => 'subPage', @@ -3582,24 +3582,21 @@ get('/:year/:month/:name', function ($year, $month, $name) { if (!login()) { file_cache($_SERVER['REQUEST_URI']); } + } else { + add_view('post_' . $name); + if (!login()) { + file_cache($_SERVER['REQUEST_URI']); + } } $post = find_post($year, $month, $name); if (is_null($post)) { - not_found(); + not_found('post_'. $name); } else { $current = $post['current']; } - if (config("views.counter") == "true") { - add_view($current->file); - - if (!login()) { - file_cache($_SERVER['REQUEST_URI']); - } - } - $author = new stdClass; $author->url = $current->authorUrl; $author->name = $current->authorName; diff --git a/system/includes/dispatch.php b/system/includes/dispatch.php index 6dd9b07..a33c001 100644 --- a/system/includes/dispatch.php +++ b/system/includes/dispatch.php @@ -407,7 +407,8 @@ function render($view, $locals = null, $layout = null) if (config('cache.timestamp') == 'true') { echo "\n" . ''; } - file_put_contents($cachefile, ob_get_contents(), LOCK_EX); + if (isset($filecache)) + file_put_contents($cachefile, ob_get_contents(), LOCK_EX); } echo trim(ob_get_clean()); } else { @@ -422,6 +423,15 @@ function json($obj, $code = 200) exit; } +function save_json_pretty($filename, $arr) +{ + if (defined("JSON_PRETTY_PRINT")) { + file_put_contents($filename, json_encode($arr, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT), LOCK_EX); + } else { + file_put_contents($filename, json_encode($arr, JSON_UNESCAPED_UNICODE), LOCK_EX); + } +} + function condition() { static $cb_map = array(); diff --git a/system/includes/functions.php b/system/includes/functions.php index d571d83..e87874c 100644 --- a/system/includes/functions.php +++ b/system/includes/functions.php @@ -525,7 +525,7 @@ function get_posts($posts, $page = 1, $perpage = 0) } if (config('views.counter') == 'true') { - $post->views = get_views($post->file); + $post->views = get_views('post_' . $post->slug, $post->file); } else { $post->views = null; } @@ -584,7 +584,7 @@ function get_pages($pages, $page = 1, $perpage = 0) $post->body = MarkdownExtra::defaultTransform(remove_html_comments($content)); if (config('views.counter') == 'true') { - $post->views = get_views($post->file); + $post->views = get_views('page_' . $post->slug, $post->file); } else { $post->views = null; } @@ -652,9 +652,9 @@ function get_subpages($sub_pages, $page = 1, $perpage = 0) // Get the contents and convert it to HTML $post->body = MarkdownExtra::defaultTransform(remove_html_comments($content)); - + if (config('views.counter') == 'true') { - $post->views = get_views($post->file); + $post->views = get_views('subpage_' . $post->parentSlug .'.'. $post->slug, $post->file); } else { $post->views = null; } @@ -1483,7 +1483,6 @@ function recent_type($type, $custom = null, $count = null) $dir = 'cache/widget'; $filename = 'cache/widget/recent.' . $type . '.cache'; $tmp = array(); - $posts = array(); if (!is_dir($dir)) { mkdir($dir, 0775, true); @@ -1522,6 +1521,7 @@ function popular_posts($custom = null, $count = null) { static $_views = array(); $tmp = array(); + $posts_list = get_blog_posts(); if (empty($count)) { $count = config('popular.count'); @@ -1539,16 +1539,19 @@ function popular_posts($custom = null, $count = null) arsort($_views); $i = 1; foreach ($_views as $key => $val) { - if (file_exists($key)) { - if (stripos($key, '/blog/') !== false && stripos($key, '/scheduled/') == false && stripos($key, '/draft/') == false) { - $tmp[] = pathinfo($key); - if ($i++ >= $count) - break; + $arr = explode('post_', $key); + if (isset($arr[1])) { + foreach($posts_list as $in => $f) { + if (strpos($f['basename'], $arr[1]) !== false ) { + $tmp[] = $f; + if ($i++ >= $count) + break; + } } - } - } + } + $dir = "cache/widget"; $filecache = "cache/widget/popular.cache"; $ar = array(); @@ -1577,8 +1580,7 @@ function popular_posts($custom = null, $count = null) echo '