summaryrefslogtreecommitdiff
blob: d28abdee023db3ceb9037ba4376f5751c9b5b4e0 (plain)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php

require_once WMP_PLUGIN_PATH . "libs/scssphp-0.3.0/scss.inc.php";
use Leafo\ScssPhp\Compiler;

if ( ! class_exists( 'WMobilePack_Themes_Config' ) ) {
    require_once(WMP_PLUGIN_PATH.'inc/class-wmp-themes-config.php');
}

if ( ! class_exists( 'WMobilePack_Themes_Compiler' ) ) {

    /**
     * Overall Themes Management class
     *
     * This class uses the SCSS compiler and it should not be included outside the admin area
     *
     * @todo Test methods from this class separately.
     *
     */
    class WMobilePack_Themes_Compiler
    {

        /* ----------------------------------*/
        /* Methods							 */
        /* ----------------------------------*/


        /**
         * Compile new css theme file.
         *
         * The method will return false (error) if:
         *
         * - it can't compile the theme because the PHP version is too old
         * - the uploads folder is not writable
         * - the variables SCSS file can't be created
         * - the CSS file can't be compiled
         *
         * @param $theme_timestamp
         *
         * @return array with the following properties:
         * - compiled = (bool) If the theme was successfully compiled
         * - error = An error message
         *
         */
        public function compile_css_file($theme_timestamp)
        {

            $response = array(
                'compiled' => false,
                'error' => false
            );

            if (!is_writable(WMP_FILES_UPLOADS_DIR)){

                $response['error'] = "Error uploading theme files, the upload folder ".WMP_FILES_UPLOADS_DIR." is not writable.";

            } else {

                $wp_uploads_dir = wp_upload_dir();
                $wmp_uploads_dir = $wp_uploads_dir['basedir'].'/';

                if (!is_writable($wmp_uploads_dir)){

                    $response['error'] = "Error uploading theme files, your uploads folder ".$wmp_uploads_dir." is not writable.";

                } else {

                    // write scss file with the colors and fonts variables
                    $generated_vars_scss = $this->generate_variables_file($error);

                    if ($generated_vars_scss) {

                        // compile css
                        $response['compiled'] = $this->generate_css_file($theme_timestamp, $error);

                        // cleanup variables file
                        $this->remove_variables_file();
                        return $response;
                    }
                }
            }

            return $response;
        }


        /**
         * Delete css file
         *
         * @param $theme_timestamp
         */
        public function remove_css_file($theme_timestamp)
        {
            $file_path = WMP_FILES_UPLOADS_DIR.'theme-'.$theme_timestamp.'.css';

            if (file_exists($file_path))
                unlink($file_path);
        }


        /**
         *
         * Write a scss file with the theme's settings (colors and fonts)
         *
         * @param bool $error
         * @return bool
         *
         */
        protected function generate_variables_file(&$error = false)
        {

            // attempt to open or create the scss file
            $file_path = WMP_FILES_UPLOADS_DIR.'_variables.scss';

            $fp = @fopen($file_path, "w");

            if ($fp !== false) {

                // read theme settings
                $theme = WMobilePack_Options::get_setting('theme');
                $color_scheme = WMobilePack_Options::get_setting('color_scheme');

                if ($color_scheme == 0){
                    $colors = WMobilePack_Options::get_setting('custom_colors');
                } else {
                    $colors = WMobilePack_Themes_Config::$color_schemes[$theme]['presets'][$color_scheme];
                }

                // write fonts
                foreach (array('headlines', 'subtitles', 'paragraphs') as $font_type){

                    $font_setting = WMobilePack_Options::get_setting('font_'.$font_type);
                    $font_family = WMobilePack_Themes_Config::$allowed_fonts[$font_setting-1];

                    fwrite($fp, '$'.$font_type."-font:'".str_replace(" ","",$font_family)."';\r\n");
                }

                // write colors
                foreach (WMobilePack_Themes_Config::$color_schemes[$theme]['vars'] as $key => $var_name){
                    fwrite($fp, '$'.$var_name.":".$colors[$key].";\r\n");
                }

                fclose($fp);
                return true;

            } else {

                $error = "Unable to compile theme, the file ".$file_path." is not writable.";
            }

            return false;
        }


        /**
         *
         * Delete variables scss file
         *
         */
        protected function remove_variables_file()
        {
            $file_path = WMP_FILES_UPLOADS_DIR.'_variables.scss';

            if (file_exists($file_path))
                unlink($file_path);
        }


        /**
         *
         * Generate a CSS file using the variables and theme SCSS files
         *
         * The CSS file is created as 'theme-{$theme_timestamp}.css' in the plugin's uploads folder.
         *
         * @param $theme_timestamp
         * @param bool|string $error
         * @return bool
         *
         */
        protected function generate_css_file($theme_timestamp, &$error = false)
        {

            // attempt to open or create the scss file
            $file_path = WMP_FILES_UPLOADS_DIR.'theme-'.$theme_timestamp.'.css';

            $fp = @fopen($file_path, "w");

            if ($fp !== false) {

                $scss_compiler = new Compiler();

                $scss_compiler->setImportPaths(array(
                    WMP_FILES_UPLOADS_DIR,
                    WMP_PLUGIN_PATH.'frontend/themes/app'.WMobilePack_Options::get_setting('theme').'/scss/'
                ));

                $scss_compiler->setFormatter('scss_formatter_compressed');

                try {

                    // write compiler output directly in the css file
                    $compiled_file = $scss_compiler->compile('@import "_variables.scss"; @import "phone.scss";');

                    fwrite($fp, $compiled_file);

                    fclose($fp);
                    return true;

                } catch (Exception $e){

                    $error = "Unable to compile theme, the theme's scss file contains errors.";
                    fclose($fp);
                }

            } else {
                $error = "Unable to compile theme, the file ".$file_path." is not writable.";
            }

            return false;
        }

    }
}