/core/admin/functions.php

https://github.com/jmrocela/Concerto · PHP · 185 lines · 129 code · 11 blank · 45 comment · 37 complexity · 33bdf641ed46f140faadd9c8c660091f MD5 · raw file

  1. <?php
  2. /*!
  3. * Concerto - a fresh and new wordpress theme framework for everyone
  4. *
  5. * http://themeconcert.com/concerto
  6. *
  7. * @version: 1.0
  8. * @package: ConcertoAdmin
  9. * @copyright: see LICENSE
  10. *
  11. * [WARNING]
  12. * This is restricted file and should not be modified in any way(unless you know what
  13. * you are doing).
  14. */
  15. /**
  16. * Export Concerto Options
  17. *
  18. * Creates a JSON file for the selected configuration context
  19. */
  20. function exportConcertoOptions($stage = 'default', $context = null){
  21. // Should add a hook somewhere to support custom option exports
  22. if (!is_array($context) && $context != null) {
  23. if ($exploded = @explode(',', $context)) {
  24. $context = array_map('trim', $exploded);
  25. } else {
  26. $val = $context;
  27. $context = array();
  28. $context[] = $val;
  29. }
  30. }
  31. $alloptions = wp_load_alloptions();
  32. $general = $design = array();
  33. foreach ($alloptions as $key => $value) {
  34. $pos = strpos($key, 'concerto_' . $stage . '_general_');
  35. if ($pos !== false ) {
  36. $general[$key] = $value;
  37. }
  38. $pos = strpos($key, 'concerto_' . $stage . '_design_');
  39. if ($pos !== false ) {
  40. $design[$key] = $value;
  41. }
  42. }
  43. $content = array();
  44. if ($context == null) {
  45. $content['general'] = $general;
  46. $content['design'] = $design;
  47. $context = array('general','design');
  48. $filename = '';
  49. } else {
  50. if (in_array('general', $context)) {
  51. $content['general'] = $general;
  52. $filename = '.general';
  53. }
  54. if (in_array('design', $context)) {
  55. $content['design'] = $design;
  56. $filename = '.design';
  57. }
  58. if (in_array('design', $context) && in_array('general', $context)) {
  59. $filename = '';
  60. }
  61. }
  62. $jsondcontent = json_encode($content);
  63. // We prepare the file for download
  64. $filename = 'concerto' . $filename . date('mdY') . '.cfwconf';
  65. header('Content-Type: application/force-download');
  66. header('Content-Type: application/octet-stream');
  67. header('Content-Disposition: attachment; filename="' . $filename . '"');
  68. header("Content-Transfer-Encoding: binary");
  69. header("Pragma: public");
  70. header("Expires: 0");
  71. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  72. ?>
  73. // This is an exported option file for Concerto generated on <?php echo date('r') ."\n"; ?>
  74. // Stage: <?php echo $stage . "\n"; ?>
  75. // Context: <?php echo implode(', ', $context) ."\n"; ?>
  76. <?php echo $jsondcontent; ?>
  77. <?php
  78. die();
  79. }
  80. /**
  81. * Import Concerto Options
  82. *
  83. * Reads a JSON file and updates the configurations based on it's values
  84. */
  85. function importConcertoOptions($file = null, $stage = 'default', $context = null) {
  86. if (is_readable($file)) {
  87. if (!is_array($context)) {
  88. if ($context != null) {
  89. if ($exploded = @explode(',', $context)) {
  90. $context = array_map('trim', $exploded);
  91. }
  92. }
  93. }
  94. $content = explode("\n", file_get_contents($file), 4);
  95. //should handle errors more thouroughly rather than suppressing them
  96. if ($options = @json_decode($content[3])) {
  97. foreach ($options as $what => $hash) {
  98. if ($context == null || in_array($what, (array) $context)) {
  99. foreach ($hash as $key => $value) {
  100. $key = preg_replace('/concerto_(.*)_' . $what . '/', 'concerto_' . $stage . '_' . $what, $key );
  101. update_option($key, $value);
  102. }
  103. }
  104. }
  105. return true;
  106. } else {
  107. return false;
  108. }
  109. }
  110. }
  111. /**
  112. * Restore Concerto Options
  113. *
  114. * Restores Default Configuration Options for Concerto depending on the Context
  115. */
  116. function restoreConcertoOptions($stage = 'default', $context = null) {
  117. include CONCERTO_LIBS . 'defaults.php';
  118. defaultOptions($stage, $context);
  119. }
  120. /**
  121. * Create Stage
  122. *
  123. * Creates a stage for concerto by name or through a stage archive
  124. */
  125. function createStage($name, $file = null) {
  126. // Check if a stage of the same name exists
  127. global $stages;
  128. foreach ($stages->stages as $stage) {
  129. if (strtolower($stage['name']) == strtolower($name)) {
  130. return 0;
  131. }
  132. }
  133. $name = ucfirst($name);
  134. if (is_writable(CONCERTO_STAGES)) {
  135. $dir = CONCERTO_STAGES . $name;
  136. if (!is_dir($dir) && !is_readable($dir)) {
  137. @mkdir($dir, 0755);
  138. }
  139. if (is_dir($dir) && is_readable($dir)) {
  140. $file = ($file != null) ? $file: CONCERTO_LIBS . 'stage' . _DS . 'default.zip';
  141. if (file_exists($file)) {
  142. if (class_exists('ZipArchive')) {
  143. $zip = new ZipArchive;
  144. if ($zip->open($file) === TRUE) {
  145. if (is_numeric($zip->locateName('style.css'))) {
  146. $zip->extractTo($dir);
  147. $zip->close();
  148. if ($zip->locateName('options.cfwconf')) { // we read the configuration file from the stage
  149. importConcertoOptions($dir . _DS . 'options.cfwconf', strtolower($name));
  150. @unlink($dir . _DS . 'options.cfwconf');
  151. }
  152. return 1;
  153. }
  154. @rmdir($dir);
  155. return 0;
  156. }
  157. }
  158. return 16; // ZipArchive class is not available
  159. }
  160. return 8; // File does not exist
  161. }
  162. return 4; // Couldn't make the stage folder
  163. }
  164. return 2; // Stage folder not writable
  165. }
  166. /**
  167. * Repair Stage Folder
  168. *
  169. * Restores Default Stage files to the default stage folder
  170. */
  171. function repairStageFolder() {
  172. createStage('default');
  173. }
  174. ?>