summaryrefslogtreecommitdiff
blob: 9ab024d93b8fd094d95a9390f1bad5176c9fa104 (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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php

if (!class_exists('WMobilePack_Uploads')) {

    /**
     * Overall Uploads Management class
     *
     * Instantiates all the uploads and offers a number of utility methods to work with the options
     *
     * @todo Test methods from this class separately
     *
     */
    class WMobilePack_Uploads
    {

        /* ----------------------------------*/
        /* Properties						 */
        /* ----------------------------------*/

        public static $allowed_files = array(
            'logo' => array(
                'max_width' => 120,
                'max_height' => 120,
                'extensions' => array('png')
            ),
            'icon' => array(
                'max_width' => 256,
                'max_height' => 256,
                'extensions' => array('jpg', 'jpeg', 'png','gif')
            ),
            'cover' => array(
                'max_width' => 1000,
                'max_height' => 1000,
                'extensions' => array('jpg', 'jpeg', 'png','gif')
            ),
            'category_icon' => array(
                'max_width' => 500,
                'max_height' => 500,
                'extensions' => array('jpg', 'jpeg', 'png','gif')
            ),
		);

		public static $manifest_sizes = array(48, 96, 144, 196);

        protected static $htaccess_template = 'frontend/sections/htaccess-template.txt';

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

        /**
         *
         * Define constants with the uploads dir paths
         *
         */
        public function define_uploads_dir()
        {
            $wp_uploads_dir = wp_upload_dir();

            $wmp_uploads_dir = $wp_uploads_dir['basedir'] . '/' . WMP_DOMAIN . '/';

            define('WMP_FILES_UPLOADS_DIR', $wmp_uploads_dir);
            define('WMP_FILES_UPLOADS_URL', $wp_uploads_dir['baseurl'] . '/' . WMP_DOMAIN . '/');

            add_action( 'admin_notices', array( $this, 'display_admin_notices' ) );
        }


        /**
         *
         * Display uploads folder specific admin notices.
         *
         */
        public function display_admin_notices()
        {
            if (!current_user_can('manage_options')) {
                return;
            }

            // if the directory doesn't exist, display notice
            if (!file_exists(WMP_FILES_UPLOADS_DIR)) {
                echo '<div class="error"><p><b>Warning!</b> The ' . WMP_PLUGIN_NAME . ' uploads folder does not exist: ' . WMP_FILES_UPLOADS_DIR . '</p></div>';
                return;
            }

            if (!is_writable(WMP_FILES_UPLOADS_DIR)) {
                echo '<div class="error"><p><b>Warning!</b> The ' . WMP_PLUGIN_NAME . ' uploads folder is not writable: ' . WMP_FILES_UPLOADS_DIR . '</p></div>';
                return;
            }
        }


        /**
         *
         * Create uploads folder
         *
         */
        public function create_uploads_dir()
        {

            $wp_uploads_dir = wp_upload_dir();

            $wmp_uploads_dir = $wp_uploads_dir['basedir'] . '/' . WMP_DOMAIN . '/';

            // check if the uploads folder exists and is writable
            if (file_exists($wp_uploads_dir['basedir']) && is_dir($wp_uploads_dir['basedir']) && is_writable($wp_uploads_dir['basedir'])) {

                // if the directory doesn't exist, create it
                if (!file_exists($wmp_uploads_dir)) {

                    mkdir($wmp_uploads_dir, 0777);

                    // add .htaccess file in the uploads folder
                    $this->set_htaccess_file();
                }
            }
        }


        /**
         *
         * Clean up the uploads dir when the plugin is uninstalled
         *
         */
        public function remove_uploads_dir()
        {

            foreach (array('icon', 'logo', 'cover') as $image_type) {

				$image_path = WMobilePack_Options::get_setting($image_type);

				if ($image_path != '' && $image_type == 'icon') {
					foreach (self::$manifest_sizes as $manifest_size) {
						$this->remove_uploaded_file($manifest_size . $image_path);
					}
				}

                $this->remove_uploaded_file($image_path);
            }

            // remove categories images
            $categories_details = WMobilePack_Options::get_setting('categories_details');

            if (is_array($categories_details) && !empty($categories_details)) {

                foreach ($categories_details as $category_id => $category_details) {

                    if (is_array($category_details) && array_key_exists('icon', $category_details)) {
                        $this->remove_uploaded_file($category_details['icon']);
                    }
                }
            }

            // remove compiled css file (if it exists)
            $theme_timestamp = WMobilePack_Options::get_setting('theme_timestamp');

            if ($theme_timestamp != ''){

                if ( ! class_exists( 'WMobilePack_Themes_Compiler' ) && version_compare(PHP_VERSION, '5.3') >= 0 ) {
                    require_once(WMP_PLUGIN_PATH.'inc/class-wmp-themes-compiler.php');
                }

                if (class_exists('WMobilePack_Themes_Compiler')) {

                    $wmp_themes = new WMobilePack_Themes_Compiler();
                    $wmp_themes->remove_css_file($theme_timestamp);
                }
            }

            // remove htaccess file
            $this->remove_htaccess_file();

            // delete folder
            rmdir(WMP_FILES_UPLOADS_DIR);
        }


        /**
         * Check if a file path exists in the uploads folder and returns its url.
         *
         * @param $file_path
         * @return string
         */
        public function get_file_url($file_path){

            if (file_exists(WMP_FILES_UPLOADS_DIR.$file_path)){
                return WMP_FILES_UPLOADS_URL.$file_path;
            }

            return '';
        }

        /**
         * Delete an uploaded file
         *
         * @param $file_path
         * @return bool
         *
         */
        public function remove_uploaded_file($file_path){

            // check the file exists and remove it
            if ($file_path != ''){
                if (file_exists(WMP_FILES_UPLOADS_DIR.$file_path))
                    return unlink(WMP_FILES_UPLOADS_DIR.$file_path);
            }
        }

        /**
         *
         * Create a .htaccess file with rules for compressing and caching static files for the plugin's upload folder
         * (css, images)
         *
         * @return bool
         *
         */
        protected function set_htaccess_file()
        {
            $file_path = WMP_FILES_UPLOADS_DIR.'.htaccess';

            if (!file_exists($file_path)){

                if (is_writable(WMP_FILES_UPLOADS_DIR)){

                    $template_path = WMP_PLUGIN_PATH.self::$htaccess_template;

                    if (file_exists($template_path)){

                        $fp = @fopen($file_path, "w");
                        fwrite($fp, file_get_contents($template_path));
                        fclose($fp);

                        return true;
                    }
                }
            }

            return false;
        }

        /**
         *
         * Remote .htaccess file with rules for compressing and caching static files for the plugin's upload folder
         * (css, images)
         *
         * @return bool
         *
         */
        protected function remove_htaccess_file()
        {

            $file_path = WMP_FILES_UPLOADS_DIR.'.htaccess';

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