PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/aurum/inc/lib/laborator/laborator-demo-content-importer/widget-importer-exporter-new/includes/import.php

https://gitlab.com/pongpanich/niyomprathai
PHP | 298 lines | 149 code | 60 blank | 89 comment | 27 complexity | 0f4063567e59d05655ac5764a3038c79 MD5 | raw file
  1. <?php
  2. /**
  3. * Import Functions
  4. *
  5. * @package Widget_Importer_Exporter
  6. * @subpackage Functions
  7. * @copyright Copyright (c) 2013 - 2015, DreamDolphin Media, LLC
  8. * @link https://github.com/stevengliebe/widget-importer-exporter
  9. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  10. * @since 0.3
  11. */
  12. // No direct access
  13. if ( ! defined( 'ABSPATH' ) ) exit;
  14. /**
  15. * Upload import file
  16. *
  17. * @since 0.3
  18. */
  19. function wie_upload_import_file() {
  20. // Check nonce for security since form was posted
  21. if ( ! empty( $_POST ) && ! empty( $_FILES['wie_import_file'] ) && check_admin_referer( 'wie_import', 'wie_import_nonce' ) ) { // check_admin_referer prints fail page and dies
  22. // Uploaded file
  23. $uploaded_file = $_FILES['wie_import_file'];
  24. // Check file type
  25. // This will also fire if no file uploaded
  26. $wp_filetype = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'], false );
  27. if ( 'wie' != $wp_filetype['ext'] && ! wp_match_mime_types( 'wie', $wp_filetype['type'] ) ) {
  28. wp_die(
  29. __( 'You must upload a <b>.wie</b> file generated by this plugin.', 'widget-importer-exporter' ),
  30. '',
  31. array( 'back_link' => true )
  32. );
  33. }
  34. // Check and move file to uploads dir, get file data
  35. // Will show die with WP errors if necessary (file too large, quota exceeded, etc.)
  36. $overrides = array( 'test_form' => false );
  37. $file_data = wp_handle_upload( $uploaded_file, $overrides );
  38. if ( isset( $file_data['error'] ) ) {
  39. wp_die(
  40. $file_data['error'],
  41. '',
  42. array( 'back_link' => true )
  43. );
  44. }
  45. // Process import file
  46. wie_process_import_file( $file_data['file'] );
  47. }
  48. }
  49. add_action( 'load-tools_page_widget-importer-exporter', 'wie_upload_import_file' );
  50. /**
  51. * Process import file
  52. *
  53. * This parses a file and triggers importation of its widgets.
  54. *
  55. * @since 0.3
  56. * @param string $file Path to .wie file uploaded
  57. * @global string $wie_import_results
  58. */
  59. function wie_process_import_file( $file ) {
  60. global $wie_import_results;
  61. // File exists?
  62. if ( ! file_exists( $file ) ) {
  63. wp_die(
  64. __( 'Import file could not be found. Please try again.', 'widget-importer-exporter' ),
  65. '',
  66. array( 'back_link' => true )
  67. );
  68. }
  69. // Get file contents and decode
  70. $data = file_get_contents( $file );
  71. $data = json_decode( $data );
  72. // Delete import file
  73. //unlink( $file );
  74. // Import the widget data
  75. // Make results available for display on import/export page
  76. $wie_import_results = wie_import_data( $data );
  77. }
  78. /**
  79. * Import widget JSON data
  80. *
  81. * @since 0.4
  82. * @global array $wp_registered_sidebars
  83. * @param object $data JSON widget data from .wie file
  84. * @return array Results array
  85. */
  86. function wie_import_data( $data ) {
  87. global $wp_registered_sidebars;
  88. // Have valid data?
  89. // If no data or could not decode
  90. if ( empty( $data ) || ! is_object( $data ) ) {
  91. wp_die(
  92. __( 'Import data could not be read. Please try a different file.', 'widget-importer-exporter' ),
  93. '',
  94. array( 'back_link' => true )
  95. );
  96. }
  97. // Hook before import
  98. do_action( 'wie_before_import' );
  99. $data = apply_filters( 'wie_import_data', $data );
  100. // Get all available widgets site supports
  101. $available_widgets = wie_available_widgets();
  102. // Get all existing widget instances
  103. $widget_instances = array();
  104. foreach ( $available_widgets as $widget_data ) {
  105. $widget_instances[$widget_data['id_base']] = get_option( 'widget_' . $widget_data['id_base'] );
  106. }
  107. // Begin results
  108. $results = array();
  109. // Loop import data's sidebars
  110. foreach ( $data as $sidebar_id => $widgets ) {
  111. // Skip inactive widgets
  112. // (should not be in export file)
  113. if ( 'wp_inactive_widgets' == $sidebar_id ) {
  114. continue;
  115. }
  116. // Check if sidebar is available on this site
  117. // Otherwise add widgets to inactive, and say so
  118. if ( isset( $wp_registered_sidebars[$sidebar_id] ) ) {
  119. $sidebar_available = true;
  120. $use_sidebar_id = $sidebar_id;
  121. $sidebar_message_type = 'success';
  122. $sidebar_message = '';
  123. } else {
  124. $sidebar_available = false;
  125. $use_sidebar_id = 'wp_inactive_widgets'; // add to inactive if sidebar does not exist in theme
  126. $sidebar_message_type = 'error';
  127. $sidebar_message = __( 'Sidebar does not exist in theme (using Inactive)', 'widget-importer-exporter' );
  128. }
  129. // Result for sidebar
  130. $results[$sidebar_id]['name'] = ! empty( $wp_registered_sidebars[$sidebar_id]['name'] ) ? $wp_registered_sidebars[$sidebar_id]['name'] : $sidebar_id; // sidebar name if theme supports it; otherwise ID
  131. $results[$sidebar_id]['message_type'] = $sidebar_message_type;
  132. $results[$sidebar_id]['message'] = $sidebar_message;
  133. $results[$sidebar_id]['widgets'] = array();
  134. // Loop widgets
  135. foreach ( $widgets as $widget_instance_id => $widget ) {
  136. $fail = false;
  137. // Get id_base (remove -# from end) and instance ID number
  138. $id_base = preg_replace( '/-[0-9]+$/', '', $widget_instance_id );
  139. $instance_id_number = str_replace( $id_base . '-', '', $widget_instance_id );
  140. // Does site support this widget?
  141. if ( ! $fail && ! isset( $available_widgets[$id_base] ) ) {
  142. $fail = true;
  143. $widget_message_type = 'error';
  144. $widget_message = __( 'Site does not support widget', 'widget-importer-exporter' ); // explain why widget not imported
  145. }
  146. // Filter to modify settings object before conversion to array and import
  147. // Leave this filter here for backwards compatibility with manipulating objects (before conversion to array below)
  148. // Ideally the newer wie_widget_settings_array below will be used instead of this
  149. $widget = apply_filters( 'wie_widget_settings', $widget ); // object
  150. // Convert multidimensional objects to multidimensional arrays
  151. // Some plugins like Jetpack Widget Visibility store settings as multidimensional arrays
  152. // Without this, they are imported as objects and cause fatal error on Widgets page
  153. // If this creates problems for plugins that do actually intend settings in objects then may need to consider other approach: https://wordpress.org/support/topic/problem-with-array-of-arrays
  154. // It is probably much more likely that arrays are used than objects, however
  155. $widget = json_decode( json_encode( $widget ), true );
  156. // Filter to modify settings array
  157. // This is preferred over the older wie_widget_settings filter above
  158. // Do before identical check because changes may make it identical to end result (such as URL replacements)
  159. $widget = apply_filters( 'wie_widget_settings_array', $widget );
  160. // Does widget with identical settings already exist in same sidebar?
  161. if ( ! $fail && isset( $widget_instances[$id_base] ) ) {
  162. // Get existing widgets in this sidebar
  163. $sidebars_widgets = get_option( 'sidebars_widgets' );
  164. $sidebar_widgets = isset( $sidebars_widgets[$use_sidebar_id] ) ? $sidebars_widgets[$use_sidebar_id] : array(); // check Inactive if that's where will go
  165. // Loop widgets with ID base
  166. $single_widget_instances = ! empty( $widget_instances[$id_base] ) ? $widget_instances[$id_base] : array();
  167. foreach ( $single_widget_instances as $check_id => $check_widget ) {
  168. // Is widget in same sidebar and has identical settings?
  169. if ( in_array( "$id_base-$check_id", $sidebar_widgets ) && (array) $widget == $check_widget ) {
  170. $fail = true;
  171. $widget_message_type = 'warning';
  172. $widget_message = __( 'Widget already exists', 'widget-importer-exporter' ); // explain why widget not imported
  173. break;
  174. }
  175. }
  176. }
  177. // No failure
  178. if ( ! $fail ) {
  179. // Add widget instance
  180. $single_widget_instances = get_option( 'widget_' . $id_base ); // all instances for that widget ID base, get fresh every time
  181. $single_widget_instances = ! empty( $single_widget_instances ) ? $single_widget_instances : array( '_multiwidget' => 1 ); // start fresh if have to
  182. $single_widget_instances[] = $widget; // add it
  183. // Get the key it was given
  184. end( $single_widget_instances );
  185. $new_instance_id_number = key( $single_widget_instances );
  186. // If key is 0, make it 1
  187. // When 0, an issue can occur where adding a widget causes data from other widget to load, and the widget doesn't stick (reload wipes it)
  188. if ( '0' === strval( $new_instance_id_number ) ) {
  189. $new_instance_id_number = 1;
  190. $single_widget_instances[$new_instance_id_number] = $single_widget_instances[0];
  191. unset( $single_widget_instances[0] );
  192. }
  193. // Move _multiwidget to end of array for uniformity
  194. if ( isset( $single_widget_instances['_multiwidget'] ) ) {
  195. $multiwidget = $single_widget_instances['_multiwidget'];
  196. unset( $single_widget_instances['_multiwidget'] );
  197. $single_widget_instances['_multiwidget'] = $multiwidget;
  198. }
  199. // Update option with new widget
  200. update_option( 'widget_' . $id_base, $single_widget_instances );
  201. // Assign widget instance to sidebar
  202. $sidebars_widgets = get_option( 'sidebars_widgets' ); // which sidebars have which widgets, get fresh every time
  203. $new_instance_id = $id_base . '-' . $new_instance_id_number; // use ID number from new widget instance
  204. $sidebars_widgets[$use_sidebar_id][] = $new_instance_id; // add new instance to sidebar
  205. update_option( 'sidebars_widgets', $sidebars_widgets ); // save the amended data
  206. // After widget import action
  207. $after_widget_import = array(
  208. 'sidebar' => $use_sidebar_id,
  209. 'sidebar_old' => $sidebar_id,
  210. 'widget' => $widget,
  211. 'widget_type' => $id_base,
  212. 'widget_id' => $new_instance_id,
  213. 'widget_id_old' => $widget_instance_id,
  214. 'widget_id_num' => $new_instance_id_number,
  215. 'widget_id_num_old' => $instance_id_number
  216. );
  217. do_action( 'wie_after_widget_import', $after_widget_import );
  218. // Success message
  219. if ( $sidebar_available ) {
  220. $widget_message_type = 'success';
  221. $widget_message = __( 'Imported', 'widget-importer-exporter' );
  222. } else {
  223. $widget_message_type = 'warning';
  224. $widget_message = __( 'Imported to Inactive', 'widget-importer-exporter' );
  225. }
  226. }
  227. // Result for widget instance
  228. $results[$sidebar_id]['widgets'][$widget_instance_id]['name'] = isset( $available_widgets[$id_base]['name'] ) ? $available_widgets[$id_base]['name'] : $id_base; // widget name or ID if name not available (not supported by site)
  229. $results[$sidebar_id]['widgets'][$widget_instance_id]['title'] = ! empty( $widget['title'] ) ? $widget['title'] : __( 'No Title', 'widget-importer-exporter' ); // show "No Title" if widget instance is untitled
  230. $results[$sidebar_id]['widgets'][$widget_instance_id]['message_type'] = $widget_message_type;
  231. $results[$sidebar_id]['widgets'][$widget_instance_id]['message'] = $widget_message;
  232. }
  233. }
  234. // Hook after import
  235. do_action( 'wie_after_import' );
  236. // Return results
  237. return apply_filters( 'wie_import_results', $results );
  238. }