forked from TheCodeDeli/stagebloc-local-theme-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
theme_view.php
144 lines (128 loc) · 5.53 KB
/
theme_view.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
require_once 'config.php'; // This will have to exist in the parent frame before this iFrame is loaded
function recursiveTemplateInclude($html, $path) {
// Find all of the Includes inside of the theme
preg_match_all('#\{(Includ[^:{}"]+)(\s[^{}]+\=[^{}]*)?\}#ims', $html, $includes);
foreach ( $includes[0] as $key => $option_variable_string )
{
if ( preg_match_all('#([a-zA-Z]*)="([^"]*)"#', $option_variable_string, $options_matches) )
{
// For each include, find which file we should be putting in its place
foreach ( $options_matches[1] as $key => $option )
{
if ( strtolower($option) === 'file' )
{
$included_template = recursiveTemplateInclude(file_get_contents($path . $options_matches[2][$key]), $path);
$html = str_replace($option_variable_string, $included_template, $html);
}
}
}
}
return $html;
}
// Get the account data returned by the API
$accounts = json_decode($accountData);
$accounts = $accounts->response->items;
$accountUrl = $customDomain = null;
$accountToUse = ( isset($_COOKIE['account']) ? $_COOKIE['account'] : $accounts[0]->id ); // Default to use the first account
foreach ( $accounts as $account ) // Get the URL of the account we're currently looking at
{
if ( $accountToUse == $account->id )
{
$customDomain = $account->custom_domain;
$accountUrl = $account->stagebloc_url;
break;
}
}
// Find all of the available themes we have
$themes = array_values(preg_grep('/^([^.])/', scandir($themePath))); // Ignore hidden files
$themeToUse = ( isset($_COOKIE['theme']) ? $_COOKIE['theme'] : $themes[0] ); // Default to use the first theme
// Get the theme and determine if there are any includes in it
$html = file_get_contents($themePath . $themeToUse . '/theme.sbt');
if ( $themeViewsPath !== null )
{
$html = recursiveTemplateInclude($html, $themePath . $themeToUse . '/' . $themeViewsPath);
}
// Pass out theme data to the API to be rendered
$postData = array(
'simulate_logged_out_user' => $simulateLoggedOutUser,
'url' => ( isset($_GET['url']) ? $_GET['url'] : '' ), // The URL of the page to render
'html' => $html
// We don't need to pass the CSS and JS since we can just add it in to the parsed theme we receive from the API and save the bandwidth
// Note: That being said, if your CSS has Option vars, you should pass it so that they are parsed by the engine. If your CSS is in seperate files, you'll need to concatenate those first
//'css' => file_get_contents($themePath . $themeToUse . '/style.css'),
//'js' => file_get_contents($themePath . $themeToUse . '/javascript.js'),
);
try
{
$renderedTheme = $stagebloc->post('theme/render', $postData);
// If we didn't pass the CSS, we'll append it to the rendered theme
if ( ! isset($postData['css']) )
{
if ( $cssPath !== null ) // If this var isn't null, we'll check another folder for the CSS files
{
// Add a <link> tag for each CSS file in the folder we linked to
$cssFiles = scandir($themePath . $themeToUse . '/' . $cssPath);
foreach ( $cssFiles as $cssFile )
{
if ( preg_match('/\.css$/', $cssFile) )
{
// This method will dump the CSS into the page itself and probably isn't very useful
//$renderedTheme = str_replace('</head>', '<style>' . file_get_contents($themePath . $themeToUse . '/' . $cssPath . $cssFile) . '</style></head>', $renderedTheme);
$renderedTheme = str_replace('</head>', '<link rel="stylesheet" type="text/css" href="' . $themePath . $themeToUse . '/' . $cssPath . $cssFile . '"></head>', $renderedTheme);
}
}
}
else
{
$renderedTheme = str_replace('</head>', '<link rel="stylesheet" type="text/css" href="' . $themePath . $themeToUse . '/style.css"></head>', $renderedTheme);
}
}
// If we didn't pass the JS, we'll append it to the rendered theme
if ( ! isset($postData['js']) )
{
if ( $jsPath !== null ) // If this var isn't null, we'll check another folder for the JS files
{
// Add a <script> tag for each JS file in the folder we linked to
$jsFiles = scandir($themePath . $themeToUse . '/' . $jsPath);
foreach ( $jsFiles as $jsFile )
{
if ( preg_match('/\.js$/', $jsFile) && ! in_array($jsFile, $jsFileBlacklist) )
{
$renderedTheme = str_replace('</head>', '<script src="' . $themePath . $themeToUse . '/' . $jsPath . $jsFile . '"></script></head>', $renderedTheme);
}
}
}
else
{
$renderedTheme = str_replace('</head>', '<script src="' . $themePath . $themeToUse . '/javascript.js"></script></head>', $renderedTheme);
}
}
// Change all the anchor tags so links render through the API instead of elsewhere
if ( $customDomain )
{
// If it's a custom domain, the replacement is a little bit different
$renderedTheme = preg_replace('#(href|action)="(http:\/\/(?:www\.)?' . $customDomain . '\/?)#', '$1="?url=', $renderedTheme);
}
$renderedTheme = preg_replace('#(href|action)="((http:\/\/stagebloc\..+?)?\/' . $accountUrl . '\/)#', '$1="?url=', $renderedTheme);
echo $renderedTheme; // Output the final result
}
catch ( Services_StageBloc_Invalid_Http_Response_Code_Exception $e )
{
die($e->getMessage());
}
?>
<script>
if ( window.self !== window.top && history.pushState ) // Only push state if we're in the iframe
{
[].forEach.call(document.getElementsByTagName('a'), function(el)
{
el.addEventListener('click', function()
{
var data = { url: '//' + location.hostname + window.location.pathname.replace('theme_view.php', '') + el.getAttribute('href') };
// Push the state of this to the parent so that refreshing the page loads the same page
parent.history.pushState(data, document.title, data.url);
});
});
}
</script>