/**
 * This code generates the angularjs html views templates
 * It uses HTTP-API with  wp_remote_get()
 * */
function angp_get_template_content($new_status, $old_status)
{
    global $post;
    if (isset($post->post_name)) {
        $post_slug = $post->post_name;
        $file = get_template_directory() . '/library/views/pages/' . $post_slug . '.html';
        if ($old_status != 'trash' && $new_status == 'trash') {
            if (file_exists($file)) {
                unlink($file);
            }
        }
    }
    if ($old_status != 'publish' && $new_status == 'publish' || did_action('wp_insert_post') === 1) {
        angp_get_page_http_api(site_url(), 'newsloop');
        $pages = get_pages(array('sort_order' => 'asc'));
        if (!empty($pages)) {
            foreach ($pages as $page) {
                $slug = $page->post_name;
                $url = get_page_link($page->ID);
                angp_set_session('index.php', 'template_req');
                angp_get_page_http_api($url, $slug);
            }
        }
    }
}
/**
 * When pages (other then the frontpage) loads the first time  then set a
 * template_req_pages session at the wp_head(for page preview) and get_header(for frontend) hook time.
 * But redirection will just happens after page is reloaded from the second time onwards with
 * redirect_other_pages_to_index_template() using 'template_include' hook.
 *
 * */
function angp_set_index_for_pages_redirect()
{
    if (is_front_page()) {
        return;
    }
    if (!isset($_SESSION['template_req_pages'])) {
        angp_set_session('index.php', 'template_req_pages');
    }
}
/**
 * Created by PhpStorm.
 * User: ROGER
 * Date: 25.02.14
 * Time: 02:02
 */
function angp_on_publish_post_delete_cache()
{
    angp_set_session('delete_post_cache_key', 'delete_post_cache_key');
}
/**
 * Set session page_loaded when page is fully loaded
 * $_COOKIE['is_page_loaded'] does the same for google chrome canary
 *
 * */
function angp_page_fully_loaded()
{
    angp_set_session('page_loaded', 'page_loaded');
}