vamtam-importers.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /*
  3. Plugin Name: VamTam Importers
  4. Description: This plugin is used in order to import the sample content for VamTam themes
  5. Version: 2.11.0
  6. Author: VamTam
  7. Author URI: http://vamtam.com
  8. */
  9. class Vamtam_Importers {
  10. public static $regenerate_thumbnails;
  11. public function __construct() {
  12. add_action( 'admin_init', array( __CLASS__, 'admin_init' ), 1 );
  13. add_action( 'plugins_loaded', array( __CLASS__, 'plugins_loaded' ) );
  14. if ( ! class_exists( 'Vamtam_Updates_2' ) ) {
  15. require 'vamtam-updates/class-vamtam-updates.php';
  16. }
  17. new Vamtam_Updates_2( __FILE__ );
  18. }
  19. public static function admin_init() {
  20. add_action( 'vamtam_before_content_import', array( __CLASS__, 'before_content_import' ) );
  21. add_action( 'vamtam_after_content_import', array( __CLASS__, 'after_content_import' ) );
  22. require 'importers/importer/importer.php';
  23. require 'importers/widget-importer/importer.php';
  24. require 'importers/revslider/importer.php';
  25. require 'importers/ninja-forms/importer.php';
  26. require 'importers/acx-coming-soon/importer.php';
  27. require 'importers/booked/importer.php';
  28. require 'importers/google-maps-easy/importer.php';
  29. }
  30. public static function plugins_loaded() {
  31. include 'wp-background-process/wp-async-request.php';
  32. include 'wp-background-process/wp-background-process.php';
  33. include 'vamtam-regenerate-thumbnails.php';
  34. self::$regenerate_thumbnails = new Vamtam_Regenerate_Thumbnails();
  35. }
  36. /**
  37. * Initialize thumbnail generation
  38. */
  39. protected static function process_thumbnails() {
  40. $attachment_ids = get_option( 'vamtam_last_import_map' )['attachments'];
  41. foreach ( $attachment_ids as $attachment_id ) {
  42. self::$regenerate_thumbnails->push_to_queue( $attachment_id );
  43. }
  44. self::$regenerate_thumbnails->save()->dispatch();
  45. }
  46. public static function before_content_import() {
  47. wp_suspend_cache_invalidation( true );
  48. self::generic_option_import( 'jetpack', array( __CLASS__, 'jetpack_import' ) );
  49. self::generic_option_import( 'foodpress', array( __CLASS__, 'foodpress_import' ) );
  50. self::generic_option_import( 'the-events-calendar', array( __CLASS__, 'tribe_events_import' ) );
  51. if ( class_exists( 'FLBuilderModel' ) ) {
  52. $post_types = get_post_types( array( 'public' => true ), 'objects' );
  53. $post_types = apply_filters( 'fl_builder_admin_settings_post_types', $post_types );
  54. $post_types = array_diff( array_keys( $post_types ), array( 'attachment', 'fl-builder-template' ) );
  55. FLBuilderModel::update_admin_settings_option( '_fl_builder_post_types', $post_types, true );
  56. FLBuilderModel::save_global_settings( json_decode( file_get_contents( VAMTAM_SAMPLES_DIR . 'beaver-global-settings.json' ) ) );
  57. FLBuilderUserAccess::save_settings( json_decode( file_get_contents( VAMTAM_SAMPLES_DIR . 'beaver-user-access.json' ), true ) );
  58. }
  59. wp_suspend_cache_invalidation( false );
  60. require 'vamtam-import-search-replace.php';
  61. }
  62. public static function after_content_import() {
  63. $map = get_option( 'vamtam_last_import_map' );
  64. $posts = get_posts( array(
  65. 'post_type' => get_post_types(),
  66. 'posts_per_page' => -1,
  67. 'meta_query' => array(
  68. 'key' => '_fl_builder_data',
  69. 'compare' => 'EXISTS',
  70. ),
  71. 'orderby' => 'ID',
  72. 'order' => 'ASC',
  73. ) );
  74. // loop through the Beaver data for all pages and map old post/term IDs to the new ones (after import)
  75. foreach ( $posts as $post ) {
  76. $data = get_post_meta( $post->ID, '_fl_builder_data', true );
  77. if ( ! $data ) {
  78. $meta = get_post_meta( $post->ID );
  79. if ( isset( $meta[ '_fl_builder_data' ] ) ) {
  80. $data = maybe_unserialize( self::fix_serialized( $meta[ '_fl_builder_data' ][0] ) );
  81. } else {
  82. echo "missing _fl_builder_data for {$post->ID} {$post->post_type}\n";
  83. unset( $data );
  84. }
  85. }
  86. if ( is_array( $data ) ) {
  87. foreach ( $data as $node_id => $node ) {
  88. // only for vamtam-blog and vamtam-projects modules
  89. if ( $node->type === 'module' && in_array( $node->settings->type, array( 'vamtam-blog', 'vamtam-projects' ), true ) ) {
  90. $settings = get_object_vars( $node->settings );
  91. foreach ( $settings as $setting_name => $setting_value ) {
  92. if ( $setting_name !== 'posts_per_page' ) {
  93. if ( strpos( $setting_name, 'posts_' ) === 0 && ! empty( $setting_value ) ) {
  94. $setting_value = explode( ',', $setting_value );
  95. foreach ( $setting_value as &$post_id ) {
  96. $post_id = $map['posts'][ (int)$post_id ];
  97. }
  98. $data[ $node_id ]->settings->{$setting_name} = implode( ',', $setting_value );
  99. } elseif ( strpos( $setting_name, 'tax_' ) === 0 && ! empty( $setting_value ) ) {
  100. $setting_value = explode( ',', $setting_value );
  101. foreach ( $setting_value as &$term_id ) {
  102. $term_id = $map['terms'][ (int)$term_id ];
  103. }
  104. $data[ $node_id ]->settings->{$setting_name} = implode( ',', $setting_value );
  105. }
  106. }
  107. }
  108. } else if ( $node->type === 'module' && $node->settings->type === 'widget' && isset( $node->settings->{"widget-nav_menu"} ) ) {
  109. $from = $node->settings->{'widget-nav_menu'}->nav_menu;
  110. if ( isset( $map['terms'][ (int) $from ] ) ) {
  111. $mapped = $map['terms'][ (int) $from ];
  112. echo "will map menu {$from} to {$mapped}\n";
  113. $data[ $node_id ]->settings->{"widget-nav_menu"}->nav_menu = $mapped;
  114. } else {
  115. echo "no mapped id for nav_menu {$from}\n";
  116. }
  117. }
  118. }
  119. update_post_meta( $post->ID, '_fl_builder_data', $data );
  120. }
  121. }
  122. self::process_thumbnails();
  123. self::megamenu_import();
  124. }
  125. public static function set_menu_locations() {
  126. if ( get_option( 'vamtam_last_import_map', false ) && ! get_option( 'vamtam_force_demo_menu', false ) ) {
  127. // assign menus to locations
  128. $main_menu = get_term_by( 'slug', 'main-menu', 'nav_menu' );
  129. $top_menu = get_term_by( 'slug', 'top-menu', 'nav_menu' );
  130. $menu_locations = get_nav_menu_locations();
  131. if ( $main_menu ) {
  132. $menu_locations['menu-header'] = $main_menu->term_id;
  133. }
  134. if ( $top_menu ) {
  135. $menu_locations['menu-top'] = $top_menu->term_id;
  136. }
  137. set_theme_mod( 'nav_menu_locations', $menu_locations );
  138. update_option( 'vamtam_force_demo_menu', true );
  139. }
  140. }
  141. public static function generic_option_import( $file, $callback ) {
  142. $path = VAMTAM_SAMPLES_DIR . $file . '.json';
  143. if ( file_exists( $path ) ) {
  144. $settings = json_decode( file_get_contents( $path ), true );
  145. foreach ( $settings as $opt_name => $opt_val ) {
  146. update_option( $opt_name, $opt_val );
  147. }
  148. call_user_func( $callback );
  149. }
  150. }
  151. public static function megamenu_import() {
  152. $file = VAMTAM_SAMPLES_DIR . 'megamenu.json';
  153. if ( file_exists( $file ) ) {
  154. $exported = json_decode( file_get_contents( $file ), true );
  155. update_site_option( 'megamenu_themes', $exported['themes'] );
  156. update_site_option( 'megamenu_settings', $exported['settings'] );
  157. do_action( 'megamenu_after_theme_import' );
  158. }
  159. }
  160. public static function jetpack_import() {
  161. Jetpack::load_modules();
  162. if ( class_exists( 'Jetpack_Portfolio' ) ) {
  163. Jetpack_Portfolio::init()->register_post_types();
  164. }
  165. if ( class_exists( 'Jetpack_Testimonial' ) ) {
  166. Jetpack_Testimonial::init()->register_post_types();
  167. }
  168. }
  169. public static function foodpress_import() {
  170. if ( function_exists( 'foodpress_generate_options_css' ) ) {
  171. foodpress_generate_options_css();
  172. }
  173. }
  174. public static function tribe_events_import() {
  175. // no cache to regenerate at this time
  176. }
  177. /**
  178. * @return string
  179. */
  180. static public function fix_serialized( $src ) {
  181. if ( empty( $src ) ) {
  182. return $src;
  183. }
  184. $data = maybe_unserialize( $src );
  185. // return if maybe_unserialize() returns an object or array, this is good.
  186. if( is_object( $data ) || is_array( $data ) ) {
  187. return $data;
  188. }
  189. $data = preg_replace_callback( '!s:(\d+):([\\\\]?"[\\\\]?"|[\\\\]?"((.*?)[^\\\\])[\\\\]?");!', array( __CLASS__, 'fix_serial_callback' ), $src );
  190. if ( ! isset( $data ) && strlen( $data ) === 0 ) {
  191. return $src;
  192. }
  193. return $data;
  194. }
  195. /**
  196. * @return string
  197. */
  198. static public function fix_serial_callback( $matches ) {
  199. if ( ! isset( $matches[3] ) ) {
  200. return $matches[0];
  201. }
  202. return 's:' . strlen( self::unescape_mysql( $matches[3] ) ) . ':"' . self::unescape_quotes( $matches[3] ) . '";';
  203. }
  204. /**
  205. * Unescape to avoid dump-text issues.
  206. *
  207. * @access private
  208. * @return string
  209. */
  210. static private function unescape_mysql( $value ) {
  211. return str_replace( array( "\\\\", "\\0", "\\n", "\\r", "\Z", "\'", '\"' ),
  212. array( "\\", "\0", "\n", "\r", "\x1a", "'", '"' ),
  213. $value );
  214. }
  215. /**
  216. * Fix strange behaviour if you have escaped quotes in your replacement.
  217. *
  218. * @access private
  219. * @return string
  220. */
  221. static private function unescape_quotes( $value ) {
  222. return str_replace( '\"', '"', $value );
  223. }
  224. }
  225. new Vamtam_Importers;