PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/bridge/export/qode-export.php

https://gitlab.com/juanito.abelo/nlmobile
PHP | 368 lines | 235 code | 57 blank | 76 comment | 17 complexity | 8a3c33158e765cf6ac2da84ff5cc9295 MD5 | raw file
  1. <?php
  2. if (!function_exists ('add_action')) {
  3. header('Status: 403 Forbidden');
  4. header('HTTP/1.1 403 Forbidden');
  5. exit();
  6. }
  7. class Qode_Export {
  8. public $export_values = "";
  9. public function __construct() {
  10. add_action('admin_menu', array($this, 'qode_admin_export'));
  11. }
  12. /**
  13. * Method that checks 'export_option' variable in $_REQUEST
  14. * and calls adequate method
  15. */
  16. public function init_qode_export() {
  17. if(isset($_REQUEST['export_option'])) {
  18. switch($_REQUEST['export_option']) {
  19. case "widgets":
  20. $this->export_widgets_sidebars();
  21. break;
  22. case "custom_sidebars":
  23. $this->export_custom_sidebars();
  24. break;
  25. case "qode_options":
  26. $this->export_options();
  27. break;
  28. case "qode_menus":
  29. $this->export_qode_menus();
  30. break;
  31. case "setting_pages":
  32. $this->export_settings_pages();
  33. break;
  34. case "all":
  35. $this->export_all();
  36. break;
  37. default:
  38. break;
  39. }
  40. }
  41. }
  42. /**
  43. * Method that exports custom made sidebars
  44. *
  45. * @param bool $save_file whether to save to file or not
  46. * @return bool | array
  47. */
  48. public function export_custom_sidebars($save_file = false){
  49. $custom_sidebars = get_option("qode_sidebars");
  50. $output = base64_encode(serialize($custom_sidebars));
  51. return $this->save_as_txt_file("custom_sidebars.txt", $output, $save_file);
  52. }
  53. /**
  54. * Method that exports theme options
  55. *
  56. * @param bool $save_file whether to save to file or not
  57. * @return bool | array
  58. */
  59. public function export_options($save_file = false) {
  60. $qode_options = get_option("qode_options_proya");
  61. $output = base64_encode(serialize($qode_options));
  62. return $this->save_as_txt_file("options.txt", $output, $save_file);
  63. }
  64. /**
  65. * Method that exports widgets
  66. *
  67. * @param $save_file bool whether to save exported output to file
  68. * @return bool | array
  69. *
  70. * @see Qode_Export::export_sidebars
  71. * @see Qode_Export::export_widgets
  72. */
  73. public function export_widgets_sidebars($save_file = false) {
  74. $this->data = array();
  75. $this->data['sidebars'] = $this->export_sidebars();
  76. $this->data['widgets'] = $this->export_widgets();
  77. $output = base64_encode(serialize($this->data));
  78. return $this->save_as_txt_file("widgets.txt", $output, $save_file);
  79. }
  80. public function export_widgets(){
  81. global $wp_registered_widgets;
  82. $all_qode_widgets = array();
  83. foreach ($wp_registered_widgets as $qode_widget_id => $widget_params) {
  84. $all_qode_widgets[] = $widget_params['callback'][0]->id_base;
  85. }
  86. foreach ($all_qode_widgets as $qode_widget_id) {
  87. $qode_widget_data = get_option( 'widget_' . $qode_widget_id );
  88. if ( !empty($qode_widget_data) ) {
  89. $widget_datas[ $qode_widget_id ] = $qode_widget_data;
  90. }
  91. }
  92. unset($all_qode_widgets);
  93. return $widget_datas;
  94. }
  95. public function export_sidebars(){
  96. $qode_sidebars = get_option("sidebars_widgets");
  97. $qode_sidebars = $this->exclude_sidebar_keys($qode_sidebars);
  98. return $qode_sidebars;
  99. }
  100. private function exclude_sidebar_keys( $keys = array() ){
  101. if (!is_array($keys)) {
  102. return $keys;
  103. }
  104. unset($keys['wp_inactive_widgets']);
  105. unset($keys['array_version']);
  106. return $keys;
  107. }
  108. /**
  109. * Method that exports navigation menus
  110. *
  111. * @param bool $save_file whether to save file or not
  112. * @return bool | array
  113. * @see get_nav_menu_locations()
  114. */
  115. public function export_qode_menus($save_file = false){
  116. global $wpdb;
  117. $this->data = array();
  118. $locations = get_nav_menu_locations();
  119. $terms_table = $wpdb->prefix . "terms";
  120. foreach ((array)$locations as $location => $menu_id) {
  121. $menu_slug = $wpdb->get_results($wpdb->prepare("SELECT * FROM $terms_table where term_id=%d", $menu_id), ARRAY_A);
  122. if (count($menu_slug) > 0)
  123. $this->data[ $location ] = $menu_slug[0]['slug'];
  124. }
  125. $output = base64_encode(serialize( $this->data ));
  126. return $this->save_as_txt_file("menus.txt", $output, $save_file);
  127. }
  128. /**
  129. * Method that exports necessary options from Settings pages
  130. *
  131. * @param bool $save_file whether to save file or not
  132. * @return bool | array
  133. */
  134. public function export_settings_pages($save_file = false) {
  135. $qode_static_page = get_option("page_on_front");
  136. $qode_post_page = get_option("page_for_posts");
  137. $qode_show_on_front = get_option("show_on_front");
  138. $qode_settings_pages = array(
  139. 'show_on_front' => $qode_show_on_front,
  140. 'page_on_front' => $qode_static_page,
  141. 'page_for_posts' => $qode_post_page
  142. );
  143. $output = base64_encode(serialize($qode_settings_pages));
  144. return $this->save_as_txt_file("settingpages.txt", $output, $save_file);
  145. }
  146. /**
  147. * Method that exports all options and sends a zip file as an output to browser
  148. *
  149. * @see Qode_Export::export_settings_pages
  150. * @see Qode_Export::export_widgets_sidebars
  151. * @see Qode_Export::export_qode_menus
  152. * @see Qode_Export::export_custom_sidebars
  153. * @see Qode_Export::export_options
  154. */
  155. public function export_all() {
  156. global $wpdb;
  157. $files_array = array();
  158. //get all files created and all options exported
  159. $settings_file = $this->export_settings_pages(true);
  160. $widgets_file = $this->export_widgets_sidebars(true);
  161. $menus_file = $this->export_qode_menus(true);
  162. $custom_sidebar_file = $this->export_custom_sidebars(true);
  163. $options_file = $this->export_options(true);
  164. //add all generated files to files array
  165. if($settings_file) {
  166. $files_array[] = $settings_file;
  167. }
  168. if($widgets_file) {
  169. $files_array[] = $widgets_file;
  170. }
  171. if($menus_file) {
  172. $files_array[] = $menus_file;
  173. }
  174. if($custom_sidebar_file) {
  175. $files_array[] = $custom_sidebar_file;
  176. }
  177. if($options_file) {
  178. $files_array[] = $options_file;
  179. }
  180. //if we have added files to files array
  181. if(is_array($files_array) && count($files_array)) {
  182. /**
  183. * Generate zip file name. It will contain name of the folder where WP is installed
  184. * and current time stamp (for caching purposes)
  185. */
  186. $wp_dir_array = parse_url(home_url());
  187. $wp_dir_name = ltrim(end($wp_dir_array), '/');
  188. //$zip_name = $wp_dir_name.'_'.date('y-m-d').'_'.date('H').'_'.date('s').'.zip';
  189. $zip_name = $wpdb->dbname.'.zip';
  190. /**
  191. * Instantiate ZipArchive class and add all files to zip
  192. */
  193. $zip = new ZipArchive();
  194. $zip->open($zip_name, ZipArchive::CREATE);
  195. foreach($files_array as $file) {
  196. $zip->addFile($file['full_path'], $file['filename']);
  197. }
  198. $zip->close();
  199. //delete all created export files from server
  200. foreach ($files_array as $file) {
  201. if(file_exists(dirname(__FILE__).'/'.$file['filename'])) {
  202. unlink(dirname(__FILE__).'/'.$file['filename']);
  203. }
  204. }
  205. //send output to browser so user can download generated zip
  206. header('Content-Type: application/zip');
  207. header('Content-disposition: attachment; filename='.$zip_name);
  208. header('Content-Length: ' . filesize($zip_name));
  209. readfile($zip_name);
  210. }
  211. }
  212. /**
  213. * Method that saves output as a txt file. It has an option for sending file to browser
  214. * or saving it on file system
  215. *
  216. * @param $file_name string name of the file to save
  217. * @param $output string content of the file
  218. * @param bool $save_to_file whether to save file to file system or not
  219. * @return array|bool if file is saved on file system it returns an array that contains
  220. * 'full_path' of the file and 'filename' of the file. If it fails to save file it returns false
  221. */
  222. public function save_as_txt_file($file_name, $output, $save_to_file = false){
  223. if($save_to_file) {
  224. if(file_put_contents(get_template_directory().'/export/'.$file_name, $output)) {
  225. return array(
  226. 'full_path' => get_template_directory().'/export/'.$file_name,
  227. 'filename' => $file_name
  228. );
  229. }
  230. return false;
  231. } else {
  232. header("Content-type: application/text",true,200);
  233. header("Content-Disposition: attachment; filename=$file_name");
  234. header("Pragma: no-cache");
  235. header("Expires: 0");
  236. print $output;
  237. exit;
  238. }
  239. }
  240. /**
  241. * Method that adds export admin page
  242. */
  243. public function qode_admin_export() {
  244. if(isset($_REQUEST['export'])) {
  245. $this->init_qode_export();
  246. }
  247. $this->pagehook = add_menu_page(
  248. 'Qode Theme', esc_html__('Qode Export', 'qode'),
  249. 'manage_options',
  250. 'qode_options_export_page',
  251. array($this, 'qode_generate_export_page')
  252. );
  253. }
  254. /**
  255. * Method that
  256. */
  257. public function qode_generate_export_page() {
  258. ?>
  259. <div id="qode-metaboxes-general" class="wrap">
  260. <form method="post" action="">
  261. <div id="poststuff" class="metabox-holder">
  262. <div id="post-body" class="has-sidebar">
  263. <div id="post-body-content" class="has-sidebar-content">
  264. <table class="form-table">
  265. <tbody>
  266. <tr><td scope="row" width="150"><h2><?php esc_html_e('Export', 'qode'); ?></h2></td></tr>
  267. <tr valign="middle">
  268. <td>
  269. <form method="post" action="">
  270. <input type="hidden" name="export_option" value="widgets" />
  271. <input type="submit" value="Export Widgets" name="export" />
  272. </form>
  273. <br />
  274. <br />
  275. <form method="post" action="">
  276. <input type="hidden" name="export_option" value="custom_sidebars" />
  277. <input type="submit" value="Export Custom Sidebars" name="export" />
  278. </form>
  279. <br />
  280. <form method="post" action="">
  281. <input type="hidden" name="export_option" value="qode_options" />
  282. <input type="submit" value="Export Options" name="export" />
  283. </form>
  284. <br />
  285. <form method="post" action="">
  286. <input type="hidden" name="export_option" value="qode_menus" />
  287. <input type="submit" value="Export Menus" name="export" />
  288. </form>
  289. <br />
  290. <form method="post" action="">
  291. <input type="hidden" name="export_option" value="setting_pages" />
  292. <input type="submit" value="Export Setting Pages" name="export" />
  293. </form>
  294. <br />
  295. <form method="post" action="">
  296. <input type="hidden" name="export_option" value="all" />
  297. <input type="submit" value="Export all" name="export" />
  298. </form>
  299. </td>
  300. </tr>
  301. </tbody>
  302. </table>
  303. </div>
  304. </div>
  305. <br class="clear"/>
  306. </div>
  307. </form>
  308. </div>
  309. <?php }
  310. }
  311. $my_Qode_Export = new Qode_Export();