summaryrefslogtreecommitdiff
blob: 5b97e2e0c63fcd504a50a5e587c2ee99e0ee16f9 (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
<?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')
            ),
        );

        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 (self::$allowed_files as $image_type => $image_settings) {

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

                if ($image_path != '' && file_exists(WMP_FILES_UPLOADS_DIR . $image_path))
                    unlink(WMP_FILES_UPLOADS_DIR . $image_path);
            }

            // 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);
        }

        
        /**
         *
         * 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);
            }
        }
    }
}