GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
import-functions.php
Go to the documentation of this file.
1 <?php
2 /**
3  * Functions related to importing exported Views.
4  *
5  * @since 2.12.1
6  * @license GPL2+
7  * @author GravityView <[email protected]>
8  * @link http://gravityview.co
9  * @copyright Copyright 2021, Katz Web Services, Inc.
10  * @package GravityView
11  */
12 
13 add_action( 'wp_import_post_meta', 'gravityview_import_helper_fix_line_breaks', 10, 3 );
14 
15 /**
16  * Fixes broken serialization character counts when new line characters are in the exported XML
17  *
18  * The XML export file includes the line breaks, which are one character when interpreted by PHP ("\n").
19  * For some reason, which I (Zack) cannot understand, the serialized data for the post meta counts both characters
20  * when generating the string length calculations.
21  *
22  * For example, the following should be exported with a string length of two: an exclamation mark and a new line ("\n")
23  * character. But it's stored in the serialized data as length of three:
24  *
25  * <example>
26  * s:3:"!
27  * ";
28  * </example>
29  *
30  * So this function replaces new line characters ("\n") in the XML and replaces them with *two* line break characters
31  * in order to match the expected number of characters. I chose this route instead of using placeholders like [newline]
32  * in case the second part (updating the meta after import) doesn't work. Multiple new lines is a better outcome than
33  * modified text.
34  *
35  * Replacing one new line with two makes the maybe_unserialize() function happy. I'm happy because we fixed the bug.
36  *
37  * Am I thrilled with this solution? No, no I am not.
38  *
39  * Does it work? Yes. Yes, it does.
40  *
41  * @since 2.12.1
42  *
43  * @param array $postmeta Copy of $post['postmeta'] to be filtered.
44  * @param int $post_id
45  * @param array $post
46  *
47  * @return array Modified array, if GravityView
48  */
49 function gravityview_import_helper_fix_line_breaks( $postmeta = array(), $post_id = 0, $post = array() ) {
50 
51  if ( empty( $post['postmeta'] ) ) {
52  return $postmeta;
53  }
54 
55  if ( 'gravityview' !== $post['post_type'] ) {
56  return $postmeta;
57  }
58 
59  $keys_to_fix = array(
60  '_gravityview_directory_fields',
61  '_gravityview_directory_widgets',
62  );
63 
64  $performed_fix = false;
65 
66  foreach ( $postmeta as &$meta ) {
67  $key = $meta['key'];
68 
69  if ( ! in_array( $key, $keys_to_fix, true ) ) {
70  continue;
71  }
72 
73  $is_valid_serialized_data = maybe_unserialize( $meta['value'] );
74 
75  // The values are not corrupted serialized data. No need to fix.
76  if ( false !== $is_valid_serialized_data ) {
77  continue;
78  }
79 
80  $meta['value'] = str_replace( "\n", "\n\n", $meta['value'] );
81 
82  $performed_fix = true;
83  }
84 
85  // Leave a note that this modification has been done. We'll use it later.
86  if ( $performed_fix ) {
87  $postmeta[] = array(
88  'key' => '_gravityview_fixed_import_serialization',
89  'value' => 1,
90  );
91  }
92 
93  return $postmeta;
94 }
95 
96 add_action( 'import_post_meta', 'gravityview_import_helper_restore_line_breaks', 10, 3 );
97 
98 /**
99  * Restores the single new line for imported Views that have been modified.
100  *
101  * @since 2.12.1
102  *
103  * @see gravityview_import_helper_fix_line_breaks()
104  *
105  * @param int $post_id
106  * @param string $key
107  * @param mixed $value
108  */
110 
111  $keys_to_fix = array(
112  '_gravityview_directory_fields',
113  '_gravityview_directory_widgets',
114  );
115 
116  if ( ! in_array( $key, $keys_to_fix, true ) ) {
117  return;
118  }
119 
120  if ( false === get_post_meta( $post_id, '_gravityview_fixed_import_serialization' ) ) {
121  return;
122  }
123 
124  if ( empty( $value ) || ! is_string( $value ) ) {
125  return;
126  }
127 
128  if ( false === strpos( $value, "\n\n" ) ) {
129  return;
130  }
131 
132  // Restore the single new line.
133  $updated_value = str_replace( "\n\n", "\n", $value );
134 
135  update_post_meta( $updated_value, $key, $updated_value );
136 }
global $post
Definition: delete-entry.php:7
gravityview_import_helper_fix_line_breaks( $postmeta=array(), $post_id=0, $post=array())
Fixes broken serialization character counts when new line characters are in the exported XML...
gravityview_import_helper_restore_line_breaks( $post_id, $key, $value)
Restores the single new line for imported Views that have been modified.