/** * Attempt to initiate WP_Filesystem * * If this fails, $use_filesystem is set to false and all methods in this class should use native php fallbacks * Thwarts `request_filesystem_credentials()` attempt to display a form for obtaining creds from users * * TODO: provide notice and input in wp-admin for users when this fails */ public function maybe_init_wp_filesystem() { ob_start(); $this->credentials = request_filesystem_credentials('', '', false, false, null); $ob_contents = ob_get_contents(); ob_end_clean(); if (wp_filesystem($this->credentials)) { global $wp_filesystem; $this->wp_filesystem = $wp_filesystem; $this->use_filesystem = true; } }
public function get_screenshot($slug, $file = 'index.html', $size = 300) { $fileuri = $this->url . '/' . $slug . '/' . $file; $screenshotfile = MYMAIL_UPLOAD_DIR . '/screenshots/' . $slug . '_' . $file . '.jpg'; $screenshoturi = MYMAIL_UPLOAD_URI . '/screenshots/' . $slug . '_' . $file . '.jpg'; $file = $this->path . '/' . $slug . '/' . $file; //serve saved if (file_exists($screenshotfile) && file_exists($file) && filemtime($file) < filemtime($screenshotfile)) { $url = $screenshoturi . '?c=' . filemtime($screenshotfile); } else { if (!file_exists($file) || substr($_SERVER['REMOTE_ADDR'], 0, 4) == '127.' || $_SERVER['REMOTE_ADDR'] == '::1') { $url = 'http://s.wordpress.com/wp-content/plugins/mshots/default.gif'; } else { $url = 'http://s.wordpress.com/mshots/v1/' . urlencode($fileuri . '?c=' . md5_file($file)) . '?w=' . $size; $remote = wp_remote_get($url, array('redirection' => 0)); if (wp_remote_retrieve_response_code($remote) == 200) { wp_filesystem(); global $wp_filesystem; if (!is_dir(dirname($screenshotfile))) { wp_mkdir_p(dirname($screenshotfile)); } $wp_filesystem->put_contents($screenshotfile, wp_remote_retrieve_body($remote), false); } } } return $url; }