PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 3.9.9
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v3.9.9
4.0.3 4.0.2 4.0.1 4.0.0 3.16.6 3.16.5 3.16.4 3.16.3 3.16.2 3.16.1 3.16.0 3.15.9 3.9.9 3.9.5 3.9.6 3.9.7 3.9.8 1.1.7 1.1.8 1.1.9 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 341 releases
profile-builder / add-ons-free / import-export / inc / class-pbie-import.php

class-pbie-import.php in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 3.9.9, at add-ons-free/import-export/inc/class-pbie-import.php

115 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class WPPB_ImpEx_Import {
4
5 protected $args_to_import;
6 public $import_messages = array();
7 private $j = '0';
8
9 /**
10 * this will take custom options and posttypes that will be imported to database.
11 *
12 * @param array $args_to_import custom options and posttypes to import.
13 */
14 function __construct( $args_to_import ) {
15 $this->args_to_import = $args_to_import;
16 }
17
18 /**
19 * this will save imported json.
20 *
21 * @param string $json_content imported json.
22 */
23 private function json_to_db( $json_content, $nonce ) {
24
25 if( !wp_verify_nonce( $nonce, 'wppb_import_setttings' ) ){
26 $this->import_messages[$this->j]['message'] = __( 'You are not allowed to do this!', 'profile-builder' );
27 $this->import_messages[$this->j]['type'] = 'error';
28 $this->j++;
29 return;
30 }
31
32 /* decode and put json to array */
33 $imported_array_from_json = json_decode( $json_content, true );
34 if ( $imported_array_from_json !== NULL ) {
35 $imported_options = $imported_array_from_json['options'];
36 $imported_posts = $imported_array_from_json['posts'];
37
38 /* import options to database */
39 foreach( $imported_options as $key => $value ) {
40
41 if( ! empty( $value ) && strpos( $key, 'wppb_' ) !== false )
42 update_option( $key, $value );
43
44 }
45
46 /* import custom posts to database */
47 foreach( $this->args_to_import as $imported_post_type ) {
48
49 /* there could be the possibility that the post type doesn't exist yet so we need to register it */
50 if ( !post_type_exists( $imported_post_type ) ) {
51 register_post_type( $imported_post_type );
52 }
53
54 $db_posts = get_posts( "post_type=$imported_post_type&posts_per_page=-1" );
55 if( ! empty( $imported_posts[$imported_post_type] ) ) {
56 foreach( $imported_posts[$imported_post_type] as $imported_post ) {
57 foreach( $db_posts as $db_post ) {
58 if( $imported_post["post_title"] == $db_post->post_title && $imported_post["post_name"] == $db_post->post_name ) {
59 wp_delete_post( $db_post->ID, $force_delete = true );
60 }
61 }
62 unset( $imported_post["ID"] );
63 $imported_post_id = wp_insert_post( $imported_post );
64 foreach( $imported_post["postmeta"] as $key => $value ) {
65 foreach( $value as $value_key => $serialized_value ) {
66 add_post_meta( $imported_post_id, $key, maybe_unserialize( $serialized_value ) );
67 }
68 }
69 }
70 }
71 }
72 } else {
73 $this->import_messages[$this->j]['message'] = __( 'Uploaded file is not valid json!', 'profile-builder' );
74 $this->import_messages[$this->j]['type'] = 'error';
75 $this->j++;
76 }
77 }
78
79 /* upload json file function */
80 public function upload_json_file() {
81 if( isset( $_POST['cozmos-import'] ) && isset( $_POST['wppb_nonce'] ) && wp_verify_nonce( sanitize_text_field( $_POST['wppb_nonce'] ), 'wppb_import_setttings' ) ) {
82 if( ( !is_multisite() && current_user_can( apply_filters( 'wppb_settings_import_user_capability', 'manage_options' ) ) ) ||
83 ( is_multisite() && current_user_can( apply_filters( 'wppb_multi_settings_import_user_capability', 'manage_network' ) ) ) ) {
84 if (!empty($_FILES['cozmos-upload']['tmp_name'])) {
85 $json_content = file_get_contents($_FILES['cozmos-upload']['tmp_name']); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
86 /* save uploaded file to server (for later versions).
87 $target = dirname( plugin_dir_path( __FILE__ ) ) . '/upload/';
88 $target = $target . basename( $_FILES['cozmos-upload']['name'] );
89 move_uploaded_file( $_FILES['cozmos-upload']['tmp_name'], $target );
90 */
91 $this->json_to_db( $json_content, sanitize_text_field( $_POST['wppb_nonce'] ) );
92 if (empty($this->pbie_import_messages)) {
93 $this->import_messages[$this->j]['message'] = __('Import successfully!', 'profile-builder');
94 $this->import_messages[$this->j]['type'] = 'updated';
95 $this->j++;
96 flush_rewrite_rules(false);
97 }
98 } else {
99 $this->import_messages[$this->j]['message'] = __('Please select a .json file to import!', 'profile-builder');
100 $this->import_messages[$this->j]['type'] = 'error';
101 $this->j++;
102 }
103 } else {
104 $this->import_messages[$this->j]['message'] = __('You do not have the capabilities required to do this!', 'profile-builder');
105 $this->import_messages[$this->j]['type'] = 'error';
106 $this->j++;
107 }
108 }
109 }
110
111 /* messages return function */
112 public function get_messages() {
113 return $this->import_messages;
114 }
115 }