PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 4.0.3
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v4.0.3
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 4.0.3, at add-ons-free/import-export/inc/class-pbie-import.php

210 lines 7.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // Exit if accessed directly
3 if ( ! defined( 'ABSPATH' ) ) exit;
4
5 class WPPB_ImpEx_Import {
6
7 protected $args_to_import;
8 public $import_messages = array();
9 private $j = '0';
10
11 function __construct( $args_to_import ) {
12 $this->args_to_import = $args_to_import;
13 }
14
15 private function json_to_db( $json_content, $nonce ) {
16
17 if( !wp_verify_nonce( $nonce, 'wppb_import_setttings' ) ){
18 $this->import_messages[$this->j]['message'] = __( 'You are not allowed to do this!', 'profile-builder' );
19 $this->import_messages[$this->j]['type'] = 'error';
20 $this->j++;
21 return;
22 }
23
24 /* decode and put json to array */
25 $imported_array_from_json = json_decode( $json_content, true );
26 if ( $imported_array_from_json !== NULL ) {
27 $imported_options = $imported_array_from_json['options'];
28 $imported_posts = $imported_array_from_json['posts'];
29
30 foreach( $imported_options as $key => $value ) {
31
32 if( ! empty( $value ) && strpos( $key, 'wppb_' ) !== false )
33 update_option( $key, $value );
34
35 }
36
37 // Suppress form-builder projection during insert — empty post_content would wipe fields.
38 if ( function_exists( 'wppb_fb_set_importing' ) ) {
39 wppb_fb_set_importing( true );
40 }
41
42 $imported_default_ids = array();
43
44 foreach( $this->args_to_import as $imported_post_type ) {
45
46 if ( !post_type_exists( $imported_post_type ) ) {
47 register_post_type( $imported_post_type );
48 }
49
50 $db_posts = get_posts( "post_type=$imported_post_type&posts_per_page=-1" );
51 if( ! empty( $imported_posts[$imported_post_type] ) ) {
52 foreach( $imported_posts[$imported_post_type] as $imported_post ) {
53 foreach( $db_posts as $db_post ) {
54 if( $imported_post["post_title"] == $db_post->post_title && $imported_post["post_name"] == $db_post->post_name ) {
55 wp_delete_post( $db_post->ID, $force_delete = true );
56 }
57 }
58 unset( $imported_post["ID"] );
59 $imported_post_id = wp_insert_post( $imported_post );
60 foreach( $imported_post["postmeta"] as $key => $value ) {
61 // Replace, don't append — get_post_meta(..., true) would otherwise return a stale first row.
62 delete_post_meta( $imported_post_id, $key );
63 foreach( $value as $raw_value ) {
64 add_post_meta( $imported_post_id, $key, $this->restore_meta_value( $raw_value ) );
65 }
66 }
67
68 if ( ! empty( $imported_post["postmeta"]["_pbform_is_default"] ) ) {
69 foreach( (array) $imported_post["postmeta"]["_pbform_is_default"] as $wppb_default_flag ) {
70 if ( $this->restore_meta_value( $wppb_default_flag ) === '1' ) {
71 $imported_default_ids[ $imported_post_type ] = $imported_post_id;
72 break;
73 }
74 }
75 }
76 }
77 }
78 }
79
80 if ( function_exists( 'wppb_fb_set_importing' ) ) {
81 wppb_fb_set_importing( false );
82 }
83
84 // Repoint wppb_default_form_ids — new IDs are not in the export.
85 $this->reconcile_default_forms( $imported_default_ids );
86 } else {
87 $this->import_messages[$this->j]['message'] = __( 'Uploaded file is not valid json!', 'profile-builder' );
88 $this->import_messages[$this->j]['type'] = 'error';
89 $this->j++;
90 }
91 }
92
93 /**
94 * Decode exported post meta without loading PHP classes.
95 *
96 * Export writes get_post_custom() strings, which are often PHP-serialized arrays.
97 *
98 * @param mixed $value Raw meta value from decoded JSON.
99 * @return mixed Scalar or array for add_post_meta.
100 */
101 private function restore_meta_value( $value ) {
102 if ( ! is_string( $value ) || ! is_serialized( $value ) ) {
103 return $value;
104 }
105
106 $restored = @unserialize( trim( $value ), array( 'allowed_classes' => false ) );
107
108 return $this->drop_incomplete_objects( $restored );
109 }
110
111 /**
112 * Replace objects PHP refused to load with an empty string.
113 *
114 * @param mixed $value Unserialized value.
115 * @return mixed
116 */
117 private function drop_incomplete_objects( $value ) {
118 if ( $value instanceof \__PHP_Incomplete_Class ) {
119 return '';
120 }
121
122 if ( is_array( $value ) ) {
123 foreach ( $value as $key => $item ) {
124 $value[ $key ] = $this->drop_incomplete_objects( $item );
125 }
126 }
127
128 return $value;
129 }
130
131 /**
132 * Point wppb_default_form_ids at imported defaults and demote any other default for the same type.
133 *
134 * @param array $imported_default_ids Map of CPT slug => imported post ID.
135 */
136 private function reconcile_default_forms( $imported_default_ids ) {
137 if ( ! function_exists( 'wppb_fb_ensure_default_forms' ) || empty( $imported_default_ids ) ) {
138 return;
139 }
140
141 $slots = array(
142 'wppb-rf-cpt' => 'register',
143 'wppb-epf-cpt' => 'edit_profile',
144 );
145
146 $defaults = (array) get_option( 'wppb_default_form_ids', array() );
147 $seeded_slots = array();
148
149 foreach ( $slots as $cpt => $slot ) {
150 if ( empty( $imported_default_ids[ $cpt ] ) ) {
151 continue;
152 }
153
154 $keep = (int) $imported_default_ids[ $cpt ];
155 $defaults[ $slot ] = $keep;
156
157 $existing = get_posts( array(
158 'post_type' => $cpt,
159 'post_status' => 'any',
160 'numberposts' => -1,
161 'fields' => 'ids',
162 ) );
163 foreach ( $existing as $existing_id ) {
164 if ( (int) $existing_id !== $keep && get_post_meta( $existing_id, '_pbform_is_default', true ) === '1' ) {
165 // Demote other defaults; do not delete their posts.
166 delete_post_meta( $existing_id, '_pbform_is_default' );
167 }
168 }
169
170 $seeded_slots[ $cpt ] = true;
171 }
172
173 update_option( 'wppb_default_form_ids', $defaults );
174 }
175
176 public function upload_json_file() {
177 if( isset( $_POST['cozmos-import'] ) && isset( $_POST['wppb_nonce'] ) && wp_verify_nonce( sanitize_text_field( $_POST['wppb_nonce'] ), 'wppb_import_setttings' ) ) {
178 if( ( !is_multisite() && current_user_can( apply_filters( 'wppb_settings_import_user_capability', 'manage_options' ) ) ) ||
179 ( is_multisite() && current_user_can( apply_filters( 'wppb_multi_settings_import_user_capability', 'manage_network' ) ) ) ) {
180 if (!empty($_FILES['cozmos-upload']['tmp_name'])) {
181 $json_content = file_get_contents($_FILES['cozmos-upload']['tmp_name']); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
182 /* save uploaded file to server (for later versions).
183 $target = dirname( plugin_dir_path( __FILE__ ) ) . '/upload/';
184 $target = $target . basename( $_FILES['cozmos-upload']['name'] );
185 move_uploaded_file( $_FILES['cozmos-upload']['tmp_name'], $target );
186 */
187 $this->json_to_db( $json_content, sanitize_text_field( $_POST['wppb_nonce'] ) );
188 if (empty($this->pbie_import_messages)) {
189 $this->import_messages[$this->j]['message'] = __('Import successfully!', 'profile-builder');
190 $this->import_messages[$this->j]['type'] = 'updated';
191 $this->j++;
192 flush_rewrite_rules(false);
193 }
194 } else {
195 $this->import_messages[$this->j]['message'] = __('Please select a .json file to import!', 'profile-builder');
196 $this->import_messages[$this->j]['type'] = 'error';
197 $this->j++;
198 }
199 } else {
200 $this->import_messages[$this->j]['message'] = __('You do not have the capabilities required to do this!', 'profile-builder');
201 $this->import_messages[$this->j]['type'] = 'error';
202 $this->j++;
203 }
204 }
205 }
206
207 public function get_messages() {
208 return $this->import_messages;
209 }
210 }