| @@ -126,8 +126,54 @@ | ||
| 126 | 126 | $fileName = "bitform-{$formId}-formid.css"; |
| 127 | 127 | Helpers::saveFile($path, $fileName, $css, $mode); |
| 128 | 128 | } |
| 129 | 129 | |
| 130 | + /** | |
| 131 | + * First row of a Model/get() style result, or null when the lookup failed | |
| 132 | + * (WP_Error), returned an empty set, or has no index 0. Prevents | |
| 133 | + * "Attempt to read property on null" when dereferencing $result[0]->col. | |
| 134 | + * | |
| 135 | + * @param mixed $result | |
| 136 | + * @return object|null | |
| 137 | + */ | |
| 138 | + public static function firstRow($result) | |
| 139 | + { | |
| 140 | + if (is_wp_error($result) || empty($result) || !isset($result[0])) { | |
| 141 | + return null; | |
| 142 | + } | |
| 143 | + return $result[0]; | |
| 144 | + } | |
| 145 | + | |
| 146 | + /** | |
| 147 | + * Decode a JSON string to an object, guaranteed to return object|null so | |
| 148 | + * downstream `$obj->prop ?? default` reads never emit an undefined-property | |
| 149 | + * warning. Returns null for non-strings, empty strings and malformed JSON. | |
| 150 | + * | |
| 151 | + * @param mixed $json | |
| 152 | + * @return object|null | |
| 153 | + */ | |
| 154 | + public static function jsonObj($json) | |
| 155 | + { | |
| 156 | + if (!is_string($json) || '' === $json) { | |
| 157 | + return null; | |
| 158 | + } | |
| 159 | + $decoded = json_decode($json); | |
| 160 | + return is_object($decoded) ? $decoded : null; | |
| 161 | + } | |
| 162 | + | |
| 163 | + /** | |
| 164 | + * Hardened hosts disable ignore_user_abort in php.ini; PHP 8 fatals on the call instead of warning. | |
| 165 | + * | |
| 166 | + * @param bool $ignore | |
| 167 | + * @return void | |
| 168 | + */ | |
| 169 | + public static function ignoreUserAbort($ignore = true) | |
| 170 | + { | |
| 171 | + if (\function_exists('ignore_user_abort')) { | |
| 172 | + \ignore_user_abort($ignore); | |
| 173 | + } | |
| 174 | + } | |
| 175 | + | |
| 130 | 176 | public static function duplicateDbTable($oldTableName, $newTableName) |
| 131 | 177 | { |
| 132 | 178 | global $wpdb; |
| 133 | 179 | $oldTable = "{$wpdb->prefix}bitforms_{$oldTableName}"; |