WYSIJA_help_themes

The MailPoet Newsletters WYSIJA help themes class.

Defined (1)

The class is defined in the following location(s).

/helpers/themes.php  
  1. class WYSIJA_help_themes extends WYSIJA_object{ 
  2. var $extensions = array('png', 'jpg', 'jpeg', 'gif'); 
  3.  
  4. function __construct() { 
  5. parent::__construct(); 
  6.  
  7. /** 
  8. * try three different methods for http request,  
  9. * @param type $url 
  10. * @return type 
  11. */ 
  12. function getInstalled() { 
  13. $helperF=WYSIJA::get('file', 'helper'); 
  14. $filenameres=$helperF->exists('themes'); 
  15.  
  16. if(!$filenameres['result']) { 
  17. return array(); 
  18.  
  19. $installedThemes = array(); 
  20. $files = scandir($filenameres['file']); 
  21. foreach($files as $filename) { 
  22. if(!in_array($filename, array('.', '..', '.DS_Store', 'Thumbs.db', '__MACOSX')) && is_dir($filenameres['file'].DS.$filename) && file_exists($filenameres['file'].DS.$filename.DS.'style.css')) { 
  23. $installedThemes[]=$filename; 
  24.  
  25. return $installedThemes; 
  26.  
  27. /** 
  28. * Get theme information (name, thumbnail, screenshot) 
  29. * @param string $theme 
  30. * @return array 
  31. */ 
  32. function getInformation($theme) { 
  33. // allowed file extensions 
  34.  
  35. $fileHelper = WYSIJA::get('file', 'helper'); 
  36.  
  37. // scan for thumbnail 
  38. $thumbnail = NULL; 
  39. for($i = 0; $i < count($this->extensions); $i++) { 
  40. // check file presence 
  41. $result = $fileHelper->exists('themes'.DS.$theme.DS.'thumbnail.'.$this->extensions[$i]); 
  42. if($result['result'] !== FALSE) { 
  43. $thumbnail = $fileHelper->url('thumbnail.'.$this->extensions[$i], 'themes'.DS.$theme); 
  44.  
  45. // scan for screenshot 
  46. $screenshot = NULL; 
  47. $width = $height = 0; 
  48. for($i = 0; $i < count($this->extensions); $i++) { 
  49. // check file presence 
  50. $result = $fileHelper->exists('themes'.DS.$theme.DS.'screenshot.'.$this->extensions[$i]); 
  51. if($result['result'] !== FALSE) { 
  52. $screenshot = $fileHelper->url('screenshot.'.$this->extensions[$i], 'themes'.DS.$theme); 
  53. $dimensions = @getimagesize($result['file']); 
  54. if($dimensions !== FALSE) { 
  55. list($width, $height) = $dimensions; 
  56.  
  57. return array( 
  58. 'name' => $theme,  
  59. 'thumbnail' => $thumbnail,  
  60. 'screenshot' => $screenshot,  
  61. 'width' => $width,  
  62. 'height' => $height 
  63. ); 
  64.  
  65. function getStylesheet($theme) 
  66. $fileHelper = WYSIJA::get('file', 'helper'); 
  67.  
  68. $result = $fileHelper->exists('themes'.DS.$theme.DS.'style.css'); 
  69. if($result['result'] === FALSE) { 
  70. return NULL; 
  71. } else { 
  72. $stylesheet = file_get_contents($result['file']); 
  73. // clear all line breaks, tabs 
  74. $stylesheet = preg_replace('/[\n|\t|\'|\"]/', '', $stylesheet); 
  75. // remove extra spaces 
  76. $stylesheet = preg_replace('/[\s]+/', ' ', $stylesheet); 
  77. return $stylesheet; 
  78.  
  79. function getData($theme) 
  80. // allowed file extensions 
  81. $this->extensions = array('png', 'jpg', 'jpeg', 'gif'); 
  82.  
  83. $fileHelper = WYSIJA::get('file', 'helper'); 
  84.  
  85. // scan for header 
  86. $header = NULL; 
  87. for($i = 0; $i < count($this->extensions); $i++) { 
  88. // check file presence 
  89. $result = $fileHelper->exists('themes'.DS.$theme.DS.'images'.DS.'header.'.$this->extensions[$i]); 
  90. if($result['result'] !== FALSE) { 
  91. $dimensions = @getimagesize($result['file']); 
  92. if($dimensions !== FALSE and count($dimensions) >= 2) { 
  93. // if image width is inferior to the max width, adjust it 
  94. list($width, $height) = $dimensions; 
  95. $ratio = round(($width / $height) * 1000) / 1000; 
  96. $width = 600; 
  97. $height = (int)($width / $ratio); 
  98. // format data 
  99. $header = array( 
  100. 'alignment' => 'center',  
  101. 'type' => 'header',  
  102. 'text' => null,  
  103. 'image' => array( 
  104. 'src' => $fileHelper->url('header.'.$this->extensions[$i], 'themes'.DS.$theme.DS.'images'),  
  105. 'width' => $width,  
  106. 'height' => $height,  
  107. 'url' => null,  
  108. 'alt' => __("Header", WYSIJA),  
  109. 'alignment' => 'center' 
  110. ); 
  111.  
  112. // scan for footer 
  113. $footer = NULL; 
  114. for($i = 0; $i < count($this->extensions); $i++) { 
  115. // check file presence 
  116. $result = $fileHelper->exists('themes'.DS.$theme.DS.'images'.DS.'footer.'.$this->extensions[$i]); 
  117. if($result['result'] !== FALSE) { 
  118. $dimensions = @getimagesize($result['file']); 
  119. if($dimensions !== FALSE and count($dimensions) >= 2) { 
  120. // if image width is inferior to the max width, adjust it 
  121. list($width, $height) = $dimensions; 
  122. $ratio = round(($width / $height) * 1000) / 1000; 
  123. $width = 600; 
  124. $height = (int)($width / $ratio); 
  125. // format data 
  126. $footer = array( 
  127. 'alignment' => 'center',  
  128. 'type' => 'footer',  
  129. 'text' => null,  
  130. 'image' => array( 
  131. 'src' => $fileHelper->url('footer.'.$this->extensions[$i], 'themes'.DS.$theme.DS.'images'),  
  132. 'width' => $width,  
  133. 'height' => $height,  
  134. 'url' => null,  
  135. 'alt' => __('Footer', WYSIJA),  
  136. 'alignment' => 'center' 
  137. ); 
  138.  
  139. // scan for divider 
  140. $divider = NULL; 
  141. for($i = 0; $i < count($this->extensions); $i++) { 
  142. // check file presence 
  143. $result = $fileHelper->exists('themes'.DS.$theme.DS.'images'.DS.'divider.'.$this->extensions[$i]); 
  144. if($result['result'] !== FALSE) { 
  145. $dimensions = @getimagesize($result['file']); 
  146. if($dimensions !== FALSE and count($dimensions) >= 2) { 
  147. // if image width is inferior to the max width, adjust it 
  148. list($width, $height) = $dimensions; 
  149. $ratio = round(($width / $height) * 1000) / 1000; 
  150. $width = 564; 
  151. $height = (int)($width / $ratio); 
  152. // format data 
  153. $divider = array( 
  154. 'type' => 'divider',  
  155. 'src' => $fileHelper->url('divider.'.$this->extensions[$i], 'themes'.DS.$theme.DS.'images'),  
  156. 'width' => $width,  
  157. 'height' => $height 
  158. ); 
  159.  
  160. return array( 
  161. 'header' => $header,  
  162. 'footer' => $footer,  
  163. 'divider' => $divider 
  164. ); 
  165.  
  166. function getDivider($theme = 'default') { 
  167. $divider = NULL; 
  168.  
  169. if($theme === 'default') { 
  170. $dividersHelper = WYSIJA::get('dividers', 'helper'); 
  171. $divider = $dividersHelper->getDefault(); 
  172. } else { 
  173. // scan for divider 
  174. $fileHelper = WYSIJA::get('file', 'helper'); 
  175. for($i = 0; $i < count($this->extensions); $i++) { 
  176. // check file presence 
  177. $result = $fileHelper->exists('themes'.DS.$theme.DS.'images'.DS.'divider.'.$this->extensions[$i]); 
  178. if($result['result'] !== FALSE) { 
  179. $dimensions = @getimagesize($result['file']); 
  180. if($dimensions !== FALSE and count($dimensions) >= 2) { 
  181. // if image width is inferior to the max width, adjust it 
  182. list($width, $height) = $dimensions; 
  183. $ratio = round(($width / $height) * 1000) / 1000; 
  184. $width = 564; 
  185. $height = (int)($width / $ratio); 
  186. // format data 
  187. $divider = array( 
  188. 'src' => $fileHelper->url('divider.'.$this->extensions[$i], 'themes'.DS.$theme.DS.'images'),  
  189. 'width' => $width,  
  190. 'height' => $height 
  191. ); 
  192.  
  193. return $divider; 
  194.  
  195. /** 
  196. * create a temporary if needed 
  197. * @param type $ZipfileResult 
  198. * @param type $theme_key 
  199. * @return type 
  200. */ 
  201. function installTheme($ZipfileResult, $manual=false) { 
  202. $helperF=WYSIJA::get('file', 'helper'); 
  203. if(!@file_exists($ZipfileResult)) { 
  204. /** 1- make the dir where the file is supposed to be received */ 
  205.  
  206. $dirtemp=$helperF->makeDir(); 
  207. $dirtemp=str_replace("/", DS, $dirtemp); 
  208. /** 2- create a temp file */ 
  209. $tempzipfile=$dirtemp.basename($_REQUEST['theme_key']).'.zip'; 
  210.  
  211. $fp = fopen($tempzipfile, 'w'); 
  212. fwrite($fp, $ZipfileResult); 
  213. fclose($fp); 
  214. }else $tempzipfile=$ZipfileResult; 
  215.  
  216. // chmod($tempzipfile, 0777); 
  217. /** 3- unzip file*/ 
  218. $dirtheme=$helperF->makeDir('themes'); 
  219.  
  220. if(!$dirtheme) { 
  221. $upload_dir = wp_upload_dir(); 
  222. $this->error(sprintf(__('The folder "%1$s" is not writable, please change the access rights to this folder so that Mailpoet can setup itself properly.', WYSIJA), $upload_dir['basedir'])."<a target='_blank' href='http://codex.wordpress.org/Changing_File_Permissions'>".__('Read documentation', WYSIJA)."</a>"); 
  223. return false; 
  224.  
  225. //$timecreated=time(); 
  226. $timecreated = substr( md5(rand()), 0, 20); 
  227. $dirthemetemp=$helperF->makeDir('temp'.DS.'temp_'.$timecreated, 0755); 
  228.  
  229. $zipclass=WYSIJA::get('zip', 'helper'); 
  230. if(!$zipclass->unzip_wp($tempzipfile, $dirthemetemp)) { 
  231. $this->error("Error while decompressing archive."); 
  232. $helperF->rrmdir($dirthemetemp); 
  233. return false; 
  234.  
  235. /**check that there is just one folder*/ 
  236. $files = scandir($dirthemetemp); 
  237. foreach($files as $filename) { 
  238. if(!in_array($filename, array('.', '..', '.DS_Store', 'Thumbs.db')) && !is_dir($dirthemetemp.DS.$filename)) { 
  239. //there is another file in there while there should be only a folder 
  240. $this->error('In your zip there should be one folder only, with the content of your theme within.'); 
  241. $helperF->rrmdir($dirthemetemp); 
  242. return false; 
  243. }else{ 
  244. if(!in_array($filename, array('.', '..', '.DS_Store', 'Thumbs.db'))) $theme_key=$filename; 
  245.  
  246. // making sure this theme only has allowed files in its folders and subfolders 
  247. $dir_iterator = new RecursiveDirectoryIterator($dirthemetemp); 
  248. $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST); 
  249.  
  250. $allowed_extensions = array('png', 'jpg', 'jpeg', 'gif', 'css', 'txt'); 
  251.  
  252. foreach ($iterator as $file) { 
  253.  
  254. if( !$file->isDir() && !in_array( pathinfo( $file->getBasename(), PATHINFO_EXTENSION ), $allowed_extensions)) { 
  255. $this->error(sprintf('Your theme is not valid. It can only contain files that have the following extensions: "%s"', join('", "', $allowed_extensions))); 
  256. $helperF->rrmdir($dirthemetemp); 
  257. return false; 
  258.  
  259. if(!$theme_key) { 
  260. $this->error('There was an error while unzipping the file :'. esc_html($tempzipfile).' to the folder: '.esc_html($dirthemetemp)); 
  261. $helperF->rrmdir($dirthemetemp); 
  262. return false; 
  263.  
  264. unlink($tempzipfile); 
  265.  
  266. //make sure we don't overwrite existing folder 
  267. if($manual && !isset($_REQUEST['overwriteexistingtheme']) && file_exists($dirtheme.DS.$theme_key)) { 
  268. $this->error(sprintf(__('A theme called %1$s exists already. To overwrite it, tick the corresponding checkbox before uploading.', WYSIJA), '<strong>'.$theme_key.'</strong>'), 1); 
  269. $helperF->rrmdir($dirthemetemp); 
  270. return false; 
  271.  
  272. /**array of files needing to be in the package*/ 
  273. $result=true; 
  274.  
  275. $listoffilestocheck=array($theme_key, 'style.css'); 
  276.  
  277. foreach($listoffilestocheck as $keyindex=> $fileexist) { 
  278. if($keyindex==0) $testfile=$listoffilestocheck[0]; 
  279. else $testfile=$listoffilestocheck[0].DS.$fileexist; 
  280. if($manual) { 
  281. if(!file_exists($dirthemetemp.DS.$testfile)) { 
  282. //this is not a theme file let's remove it 
  283. if($keyindex==0) $this->error('Missing directory :'. esc_html($testfile)); 
  284. else $this->error('Missing file :'.$dirthemetemp.DS.esc_html($testfile)); 
  285.  
  286. $result=false; 
  287.  
  288.  
  289.  
  290. /** 2- move folder to uploads/wysija/themes/ */ 
  291. if($result) { 
  292. //once it's all good we move the theme to the right folder 
  293. $helperF->rcopy($dirthemetemp.DS.$listoffilestocheck[0], $dirtheme.DS.$listoffilestocheck[0]); 
  294.  
  295. $this->notice(sprintf(__('The theme %1$s has been installed on your site.', WYSIJA), '<strong>'. esc_html($theme_key).'</strong>')); 
  296. }else{ 
  297. $this->error(__("We could not install your theme. It appears it's not in the valid format.", WYSIJA), 1); 
  298. //remove the temporary gfolder 
  299. $helperF->rrmdir($dirthemetemp); 
  300.  
  301. return $result; 
  302.  
  303.  
  304. function delete($themekey) { 
  305.  
  306. $helperF=WYSIJA::get('file', 'helper'); 
  307. $dirtheme=$helperF->makeDir('themes'.DS.$themekey); 
  308.  
  309. $helperF->rrmdir($dirtheme); 
  310.  
  311. if(!file_exists($dirtheme)) { 
  312. $this->notice(sprintf(__('Theme %1$s has been deleted.', WYSIJA), '<strong>'.$themekey.'</strong>')); 
  313. return true; 
  314. }else{ 
  315. $this->error(sprintf(__('Theme %1$s could not be deleted.', WYSIJA), '<strong>'.$themekey.'</strong>')); 
  316. return false;